Hex to binary
ATMMAN
Posts: 3
I currently use the stamp for a keypad encoder. I need to add more functionality to it. I need to convert incoming hex text to binary, xor it with a second incoming hex stream converted to binary and output it in hex format. I know you can use the debug command to convert to hex, decimal and binary, but I need to do 2 hex bytes at a time in 8 chunks. Any ideas?
Bernie
Bernie
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
good bye
There are already "formatters" like DEC, BIN, and HEX that can convert binary information to the equivalent decimal, binary, and hexadecimal text streams. They also work the other way in the DEBUGIN and SERIN statements to convert sequences of characters to the equivalent binary values. If the keypad provides pairs of text characters, you can use the HEX2 formatter to convert 2 characters to an equivalent 8 bit binary value. Have a look at the SERIN statement description and the matching appendix for the formatters in the PBasic manual.
What I am trying to do is to combine 2 key components for injection into a encryption device. The hex is all caps with no delimiter. We have to convert it for XORing 2 hex characters at a time ( 1 byte) then convert the result back to hex for injection into the encryption box.
Bernie
If you can, use the HEX2 formatters like this:
SERIN <pin>,<Baudmode>,[noparse][[/noparse]HEX2 www, HEX2 xxx, HEX2 yyy, HEX2 zzz]
This will take a sequence of 8 characters from the input pin as serial input text, treat them as 4 bytes of 2 digit hex values, and provide you with the 4 byte values www, xxx, yyy, zzz. You can XOR them any way you want to, then output them as serial output text with:
SEROUT <pin>,<Baudmode>,[noparse][[/noparse]HEX2 www, HEX2 xxx, HEX2 yyy, HEX2 zzz]
Obviously, you can change the format any way you need to, but this is the basic scheme.
If it would save you anything without complicating your program, you can handle a 16 bit word as 2 sets of 4 hex digits like:
SERIN <pin>,<Baudmode>,[noparse][[/noparse]HEX4 mmm, HEX4 nnn]
and
SEROUT <pin>,<Baudmode>,[noparse][[/noparse]HEX4 mmm, HEX4 nnn]