Hex Value Madness
Guys
I've got myself stuck with a hex string return from a piece of broadcast kit.
It sends back the elapsed time in HEX, but the values it sends actually represent DECIMAL values
For example.....
Line 330 (12:32:58):: String From [5001:7:1]-[$74$00$49$46$01$00$04]
The actual time is 00h(char 6),01m(char 5),46s(char 4). Note the 46 - not 70 (ie decimal $46)
The only way I can think of presenting each char as 2 digit ascii text for my panels is ...
FORMAT('%02d',ATOI(ITOHEX(cData[4]))) - which seems a bit convoluted to say the least!
I'm sure I'm missing something here, but I haven't seen any daylight for 48 hours and I think it's affecting me a bit!
Thanks
Simon
I've got myself stuck with a hex string return from a piece of broadcast kit.
It sends back the elapsed time in HEX, but the values it sends actually represent DECIMAL values
For example.....
Line 330 (12:32:58):: String From [5001:7:1]-[$74$00$49$46$01$00$04]
The actual time is 00h(char 6),01m(char 5),46s(char 4). Note the 46 - not 70 (ie decimal $46)
The only way I can think of presenting each char as 2 digit ascii text for my panels is ...
FORMAT('%02d',ATOI(ITOHEX(cData[4]))) - which seems a bit convoluted to say the least!
I'm sure I'm missing something here, but I haven't seen any daylight for 48 hours and I think it's affecting me a bit!
Thanks
Simon
Comments
This looks to be Binary Coded Decimal, which is actually pretty easy to deal with. See this thread for more information. Basically, you can take the character $46, which has the equivalent integer value of 70 (16 * 4 + 6) and do a modulo operation with 16 to get the ones place, then you can divide the same number by 16 and multiply by 10 to get the ones place.
Define_function integer fnDecodeBCD(char bcd){ return (bcd/16 * 10) + (bcd % 16) } define_function char fnEncodeBCD(int bcd){ return (bcd/10 * 16) + (bcd % 10)Hope that helps.
How about this?
FORMAT('%02X', cData[4])
--D
My method does work, it just looks a mess - Andrew's BCD function would make it look a lot cleaner for sure.
Dchristo - not sure that would work? Won't that return a hex value? Or maybe that's what I want .... Hmm.
I'm off to have another try!
S