Ascii to hex
proggieus
Posts: 3
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?
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?
0
Comments
What device are you dealing with?
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
proggieus: hextoi() probably does what you want.
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!)
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
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
Enjoy
-Tim