Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Basic integer hex conversion

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Location
    Atlanta, GA
    Posts
    13

    Basic integer hex conversion

    I am using the FlexCAN library. The message array is a bunch of integers.

    In my particular case, I have two integers that actually represent a two-byte larger integer.

    The hex string '10E' (integer 270) has come through as an array with 16 (10 hex) and 14 (E hex). So, the array looks like:

    bytearray[0] = 14 // E
    bytearray[1] = 16 // 10

    I'm failing to understand how to take these two ints and convert them to the actual int. Converting the ints back to hex and then back to a big int seems odd, and I can't really figure out how to do that, either.

    Sorry, this low-level byte magic C++ is beyond me.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,845
    In the end when in a variable of the proper size - it is binary in memory. But it has been parsed as noted into two unique byte values for a larger Hex number.

    How it is displayed base 2, 10, or 16 is just output manipulation of those binary bits.

    So : uint32_t bigInt = bytearray[1] * 256 + bytearray[0];

    Will convert those two bytes of Hex into a single integer in memory.

    <edited> : 256 mult not 16
    Last edited by defragster; 01-29-2022 at 02:32 AM.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Atlanta, GA
    Posts
    13
    Your code does not seem to work for me.

    Code:
    MB 0  OVERRUN: 0  LEN: 8 EXT: 0 TS: 39395 ID: 589 Buffer: 0 0 0 0 0 1 80 0
    Lo word: 1   Hi word: 128
    Can Speed: 384
    2049
    The hex string is 0x180 split into 0x01 and 0x80 (384 = 0x180)
    0x01 is 1
    0x80 is 128

    Your code produces a value of 2049 (128*16 + 1).
    If I reverse it, it would only be 16+128 = 144.

    I had found this thread:
    https://forum.pjrc.com/threads/32592...ll=1#post95027

    Code:
      can_speed = (int16_t((hi_word) | (lo_word << 8)));
    The above produces 384, which was the input.

  4. #4
    Senior Member
    Join Date
    Aug 2018
    Location
    Brisbane, Australia
    Posts
    115
    (0x01 * 256) + 0x80 = 384

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,854
    You haven't separated the hex correctly. The hex 10E has an implied leading zero and should be 010E which in your bytearray would be
    bytearray[0] = 14 // E
    bytearray[1] = 01 // 1

    and now you can use
    bytearray[1]*256 + bytearray[0]
    which will produce 270.

    Pete

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,854
    Hmmm, or perhaps what you've called a hex string, 10E, is actually three ASCII characters and would be stored as:
    bytearray[0] = 'E';
    bytearray[1] = '0';
    bytearray[2] = '1';

    Pete

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,845
    Edited p#2 - if as described with byte value 0-255 it should have been mult of *256 not *16

    But that didn't match the math as presented 1*256+14 versus 0x10*16 + 14 to get 270 ????
    Last edited by defragster; 01-29-2022 at 04:00 AM.

  8. #8
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    606
    Or you can go to the really low level stuff
    And just put the values where they belong
    https://stackoverflow.com/questions/...e-array-to-int
    Off course then you need to know which order the data is sent,
    I.e big endian or vice versa

  9. #9
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    606
    Or just do simple casting, no unnecessary calculations needed

    Code:
    uint16_t bigInt = 0;
    byte *b = (byte *)&bigInt;
    b[0] = bytearray[0];
    b[1] = bytearray[1];
    or (depending on the byte order)
    Code:
    uint16_t bigInt = 0;
    byte *b = (byte *)&bigInt;
    b[0] = bytearray[1];
    b[1] = bytearray[0];

  10. #10
    Junior Member
    Join Date
    Dec 2021
    Location
    Atlanta, GA
    Posts
    13
    Quote Originally Posted by defragster View Post
    In the end when in a variable of the proper size - it is binary in memory. But it has been parsed as noted into two unique byte values for a larger Hex number.

    How it is displayed base 2, 10, or 16 is just output manipulation of those binary bits.

    So : uint32_t bigInt = bytearray[1] * 256 + bytearray[0];

    Will convert those two bytes of Hex into a single integer in memory.

    <edited> : 256 mult not 16
    With your edit, I think this should work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •