Shop OBEX P1 Docs P2 Docs Learn Events
Hex to binary — Parallax Forums

Hex to binary

ATMMANATMMAN Posts: 3
edited 2007-07-03 21:15 in BASIC Stamp
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

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-07-03 01:51
    Hex, Binary (and decimal or octal) is just a way to represent the data so us humans can make sense of it. It's all binary to the processor.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • ATMMANATMMAN Posts: 3
    edited 2007-07-03 03:48
    True, but that doesn't really help.

    good bye
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-03 03:55
    What kind of text are you wanting to convert? Is it straight ASCII? Are the letters upper case or lower case or either? Is there some kind of delimiter at the end, if so what?

    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.
  • ATMMANATMMAN Posts: 3
    edited 2007-07-03 21:04
    Thanks Mike. I will have a look at that.

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-03 21:15
    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]
Sign In or Register to comment.