No announcement yet.

Hex, Bytes and data

  • Filter
  • Time
  • Show
Clear All
new posts

  • Hex, Bytes and data

    I am hoping someone might be able to help. I am new to programming. I have successfully interfaced a DS18B20 digital temp sensor through a DS2482 to an RF 200 module.
    Where I am having a little problem is with the received data. The data is in two bytes, Highbyte and Lowbyte. I have combine these with the '+' operator, I have noticed that there is a space between the two values (01 3D) when printed out rather than 013D.
    As a test I ran the bitwise operation and AND on 0x013D, and the result was correct.
    So my question is how to manipulate the data (01 3D) so that the rest of the code will work on it.
    I imagine this is a real newbie question, so forgive my ignorance.
    Attached Files

  • #2
    Your snippet doesn't contain enough code to really tell what's going on. In particular, what exactly are your b2 and b3 variables - are they actually integers, or single-character strings, or two-character strings that have already been converted to hex?


    • #3
      b2 and b3 are bytes resulting from i2c read commands eg. (b2 = i2cRead (cmd, 1, retries, False)), b2 being the LowByte and b3 the HighByte.
      Hope that helps.


      • #4
        Ok, that would make them 1-character strings then. Simply adding them together would make something that's human-readable in a hex dump, but it's not a very useful format for doing any actual computation with the value. You'd want to do something like this:
            value = (ord(b3) << 8) | ord(b2)
        giving you an integer value.


        • #5
          Thanks very much for your help, working now!