Shop OBEX P1 Docs P2 Docs Learn Events
How do I convert HEX to Text? — Parallax Forums

How do I convert HEX to Text?

ChemikalChemikal Posts: 32
edited 2009-10-27 16:22 in Propeller 1
Evening,

How do I convert from hexadecimal to text (ascii, ansi)?


Example:

I have "54:78:44:61:74:61:30:41" or "5478446174613041" (no colons)
How do I have the Prop convert that to its equivalent as a string of "TxData0A"?

My favorite site for this as a reference: http://home2.paulschou.net/tools/xlate/


Thanks,
- Josh

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-27 03:19
    see the FullDuplexSerial driver from OBEX for the hex to character routine (and decimal).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)
    · Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • ChemikalChemikal Posts: 32
    edited 2009-10-27 05:06
    The only routines I see are StrToHex(stringptr) & StrToBin(stringptr)

    What I do see in another routine is the lookupz command... can I use something like that to form the string? If I can, how would I look through the hex by groups of two?

    Else, some other method?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-27 05:33
    First of all, the Prop is a 32 bit device. 32 bits is 8 hexadecimal digits like 54784461 or 74613041. Is that what you want converted to ASCII? The FullDuplexSerial driver has a "hex" routine that takes a 32 bit (long) value and converts it to ASCII characters with a specified number of hexadecimal digits. If you have two 32 bit longs, you can call the "hex" routine twice, once for each 32 bit long, specifying 8 hexadecimal digits each and you'll get a total of 16 hexadecimal digits with nothing in-between.

    If you need the colons, you'll have to do something more complicated and product 2 hexadecimal digits for each 8 bits of the 32 bit longs and output the colon yourself in the right place. You can still use the "hex" routine and just ask for 2 hexadecimal digits at a time, rotating the 32 bit value left by 8 bits (< -) before calling the "hex" routine each time.

    If you're trying to convert an ASCII hexadecimal string to binary, you need the FullDuplexSerialPlus object and you'll need to modify the StrToHex routine to ignore the colons and to produce two 32 bit long values from 16 hexadecimal digits.
  • ChemikalChemikal Posts: 32
    edited 2009-10-27 12:06
    I will need to work on my Prop to churn through what you said to see the results, but this is what I am trying to do:


    I am using XBees, which requires data to be sent in hex as a series of bytes
    I have a PHP application that I wrote, which sends various text strings to the Prop through a hardwired serial line, which come from my Google calendar
    The strings are the calendar entries, such as "CHE4115: Separation Processes", which the Prop will then send with an attached XBee to a remote Prop with a LCD attached to it
    I then need the remote Prop to take those series of bytes it received in the XBee packet, and output it in a LCD compatible format, and I was hoping to use LCD.str(Variable)

    Am I on the right track? Any suggestions on what approach I should take?


    - Josh
  • localrogerlocalroger Posts: 3,452
    edited 2009-10-27 12:42
    Chemikal, assuming you have c (the cursor) pointing at the start of your input string and p (a pointer) to go to output, this is how you do it (not tested, but should be close):
    repeat 6 'octets
      r := 0
      repeat 2
        r <<= 4
        r += lookdownz(byte[noparse][[/noparse]c++],"0123456789ABCDEF")
        byte[noparse][[/noparse]p++] := r
    

    in English:· Each byte is located in a sorted list of the possible hex chars via lookdownz (a very useful command, in other environments you have to test for whether the char is > 9 and adjust for A).· This returns the index 0-15 corresponding to the hex value.· Each output digit is initialized to zero and shifted left 4 before adding the next digit in.· This starts off by shifting some zeroes but leaves our last 4-bit value in the LSB where we want it.

    This is also easily extended by changing the repeat 2 to transfer more digits, and instead of the byte operators you could use indeces into byte arrays containing the characters.

    On review... it seems you might want to go the other way; that's very similar.· If you have the octets and you want the hex, this is what you do:

    repeat 6
      r := byte[noparse][[/noparse]c++]
      repeat 2
        byte[noparse][[/noparse]p++] := lookupz((r >> 4) & $F,"0123456789ABCDEF")
        r <<= 4
    

    The code that does this in fullduplexserial is a bit more terse but I've made this more similar to the other example so hopefully it will be clear how it works.· For each byte we start by masking the high nibble, moving it to the low nibble, then doing the lookup which returns the character at that position in the lookup list.· Then the low nibble is moved to the high nibble before doing it again, so we get the high nibble out first the way humans like to read it.

    Post Edited (localroger) : 10/27/2009 12:52:37 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-10-27 13:47
    Don't understand where you have a problem!

    You have a PC sending a string to a prop.
    The prop with the string - which is a series of bytes terminated by a zero - simply has to send the string via XBee.
    On the other side you have a prop which receives this series of bytes which still is a string. You take it and display it, as the display also is happy with a string.

    No conversion needed.
  • ChemikalChemikal Posts: 32
    edited 2009-10-27 14:27
    Ah, I think I am coming into a epiphany.

    Basically, everything is equal, as I understand it, but we're simply changing the human readable format.

    ie: $54$78$44$61$74$61$30$41··== "T""x""D""a""t""a""0""A" (will it read as "TxData0A", or will it multiply all the bytes together? Then again, "TxData0A" will equate to a large hex result as if each letter was multiplied together,·yes?)

    So I can simply do my qualifications by comparing the hex result to a quoted result (string)?

    if HexReceived == "TxData0A" (where HexReceived is the series of hex bytes...)

    I will need to look at the transmissions with my logic analyzer, to see how the data is being sent, and then set up some Prop tests to see what different things equate to (such as comparing multiplied hex bytes to strings)

    That, and I probably need more coffee... not sure if I am making sense or on the right track.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-27 14:58
    You would need to do your string compare like:

    if strcomp(@HexReceived,string("TxData0A"))

    Spin doesn't have string variables. HexReceived would have to be a byte array containing the received data terminated by a zero byte. Look up STRCOMP and STRING in the Propeller Manual.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-10-27 15:21
    Computers work with bits. As one bit can only hold a very small amount of information (0 or 1) the bits are packed together to a more useful package. In former times thes were 4 bits, later 8 bits and with the propeller we have 32 bits. Usually 8 bits are still familiar (which is called a byte). Harddrives use bytes, serial interfaces use bytes ( I mean the netto information - for syncronisation and error detection some bits are added ).

    What a byte means can be anything and it simply depends on where the byte is used. It can be a number which uses all bits or maybe only part of the bits. For example a temperature range from -30 to 50°C ... It might be an ascii character. A sequence of bytes might be a sequence of temperature measurements or a string .... or 3 bytes might be a RGB pixel in a picture. So, you need to know the format of a bytestream for correct handling.

    In your case:
    Your PHP sends a string, which is a sequence of bytes in ASCII code.
    I expect that your XBee object has a function similar to the rx / tx functions of the serial interface. You put in a byte and it will be transmitted.
    On the receiver side you receive exactly this byte. The XBee does not care about the meaning of the byte. Nor does it change anything. So, if you send a string on one side you will receive this string on the other side.
    And if you received a string you can simply call the print function of the LCD display and is shows this string.

    The only problem you might have is the detection of the end of the string. Maybe you have to append a zero in PHP or you use "\n". SPIN functions expect a zero as string terminator.
  • ChemikalChemikal Posts: 32
    edited 2009-10-27 16:22
    Awesome, thanks all for the help. I have a pretty good grasp on it all now.

    I will work it out tonight when I am in front of my Prop to further instill my method of attack on this, but I should be all set now.


    Thanks again,
    - Josh
Sign In or Register to comment.