Home AMX User Forum NetLinx Studio

Ascii to hex

i have a device that sends out an 2 byte ascii string to represent a hexidecimal number, i need to convert it to true hex so i can run some mathmatical operators on the hex.

i thought of a lookup table but there is about a 6000 number spread so that seems to be a PITA

Anybody have any ideas?

Comments

  • jjamesjjames Posts: 2,908
    ascii is hex is decimal is binary . . . the master doesn't care. You can do mathematical operations on integers, right? If you need an ascii representation of a hex value, my suggestion would be to do the math on the int, then use itohex().

    What device are you dealing with?
  • a_riot42a_riot42 Posts: 1,624
    proggieus wrote: »
    i have a device that sends out an 2 byte ascii string to represent a hexidecimal number, i need to convert it to true hex so i can run some mathmatical operators on the hex.

    i thought of a lookup table but there is about a 6000 number spread so that seems to be a PITA

    Anybody have any ideas?

    6000 number spread? I don't know what that means, but there are only 16 hex characters 0-9 and A-F. You can convert an ascii character to its hex representation by simpy subtracting 30. ASCII character '1' is $31 so if you want its hex representation subtracting 30 gets you back to the hex number $01. Perhaps I am misunderstanding your question.
    Paul
  • truetrue Posts: 307
    He gets a string like "3c" which is "$33, $64" or "fe" which is "$66, $65". He wants this in "true hex," or rather a char or int, to work with the number. What he means by "true hex" of course is just a number - but someone else can drone on about how decimal, hex, octal, binary, etc. are all different ways of saying the exact same thing =)

    proggieus: hextoi() probably does what you want.
  • jjamesjjames Posts: 2,908
    true wrote: »
    What he means by "true hex" of course is just a number - but someone else can drone on about how decimal, hex, octal, binary, etc. are all different ways of saying the exact same thing =)

    proggieus: hextoi() probably does what you want.
    I was illustrating that itoahex would be needed to send the string to the device after his mathematical operations.

    But you caught what I missed . . . he said "i have a device that sends out an 2 byte ascii string to represent a hexidecimal number", I misread it and thought the direction was AMX->Device, not vice verse. (oops!)
  • hextoi does work
    true wrote: »
    He gets a string like "3c" which is "$33, $64" or "fe" which is "$66, $65". He wants this in "true hex," or rather a char or int, to work with the number. What he means by "true hex" of course is just a number - but someone else can drone on about how decimal, hex, octal, binary, etc. are all different ways of saying the exact same thing =)

    proggieus: hextoi() probably does what you want.

    i was thinking i needed something like ATOI that actually converts the ascii strings to the integer.

    the device is a temprature and humidity probe that has temp down to .01 of a degree and humidity down to .005%
    it sends out the temp as an ascii string of a hex value which then gets converted to decimal and divided by 100 to get me a reading in C which i then have to convert to F

    so a lookup table would have ended up being thousands of entries.

    Thanks for the help
  • Pseduo Code

    Heres some psedu code that I banged out in notepad real fast as I recently had to write a function called HexToBin. It was kind of similar in an odd way where it took in a single char and returned an 8 byte array of 0s and 1s.

    Hope you find this helpful

    If (Byte2 <= $39) // 0-9 ($31 - $39)
    LSB = Byte2 - $31
    else // A-F ($61 - $66)
    LSB = Byte2 - $57

    If (Byte1 <= $39) // 0-9 ($31 - $39)
    MSB = Byte1 - $31
    else // A-F ($61 - $66)
    MSB = Byte1 - $57

    SingleHexVal = (MSB *16) + LSB

    Enjoy

    -Tim
Sign In or Register to comment.