Literal Hex to ASCII Conversion

Greetings,

I am trying to translate a literal hex string to the ASCII characters it represents.

Example: '46726F6E74' needs to be translated to 'Front'

I have looked at FORMAT, HEXTOI, etc with no luck.

Any thoughts?

Thanks.

Comments

  • I don't know a ready-to-use programming instruction which will do this.

    Assuming a single byte is represented always with 2 characters, only a self made function may do this.
    DEFINE_FUNCTION CHAR[15] AsciiHexToBytes(CHAR sStr[30])
    {
    STACK_VAR CHAR sByteAscii[2]
    STACK_VAR CHAR sHexString[15] // the output string
    STACK_VAR INTEGER nByteCount // how much real bytes
    STACK_VAR INTEGER nX // for the FOR loop
    nByteCount = LENGTH_STRING(sStr) / 2 // how much "real" Bytes are there
    FOR(nX=1;nX<=nByteCount;nX++)
    {
    sByteAscii = GET_BUFFER_STRING(sStr,2)
    sHexString = "sHexString,HEXTOI(sByteAscii)"
    }
    RETURN sHexString
    }
    

    If the byte values are matching the ASCII table, you'll get your "Text".

    Should do that - theoredically ;). Don't have a master to test at the moment.....
  • yuri
    yuri Junior Member
    ITOA(HEXTOI(strHexString))

    ;)
  • trav
    trav Get off my lawn
    Or if you are getting it in as comma seperated hex values, just bung it in a char array.
  • yuri wrote:
    ITOA(HEXTOI(strHexString))

    ;)
    Hm....
    HEXTOI('3132') -> 12594
    ITOA(12594) -> '12594'

    But if handling that 4 chars '3132' as "2 bytes" (what my function above may do), the result of the function is " '12' "

    Other example:
    HEXTOI('595A') -> 22874
    ITOA(22874) -> '22874'
    But the function result may be " 'YZ' "
  • mpullin
    mpullin Obvious Troll Account, Marked for Deletion
    I'd worry about exceeding the maximum integer size if I were doing HEXTOI on the whole thing. Marc's function seems like the way to go.
  • yuri
    yuri Junior Member
    i will post three ;) next time to denote the amount of sarcasm :p
  • mpullin
    mpullin Obvious Troll Account, Marked for Deletion
    yuri wrote:
    i will post three ;) next time to denote the amount of sarcasm :p
    umm... lol? :p
  • TurnipTruck
    TurnipTruck Junior Member
    Thank you Marc and others. I will code up the function that Marc suggests and see what happens. I did something similar in the past, but it resulted in compiler warnings.
  • yuri wrote:
    i will post three ;) next time to denote the amount of sarcasm :p

    Ok, don't see my message as "I have not seen the sarcasm" but as "I just wanted to show what's the difference for those who are interested" (n+1 :D)
  • TurnipTruck
    TurnipTruck Junior Member
    Back on topic here......

    The HEXTOI conversion works well. It may be recommended that the string which will hold the converted value not have its type defined. If declared as a CHAR array, you may get a compiler warning when you try to put an integer value into it.

    DEFINE_VARIABLE

    CHAR cORIGINAL_STRING[32]
    cCONVERTED_STRING[16]

    Thank you for your help.
  • DHawthorne
    DHawthorne Junior Member
    Back on topic here......

    The HEXTOI conversion works well. It may be recommended that the string which will hold the converted value not have its type defined. If declared as a CHAR array, you may get a compiler warning when you try to put an integer value into it.

    DEFINE_VARIABLE

    CHAR cORIGINAL_STRING[32]
    cCONVERTED_STRING[16]

    Thank you for your help.

    Not declaring a type is the same as flat -out declaring it INTEGER; that's the default variable type. Maybe I'm just being nit-picky here, but if your problem is assigning an integer value to an array position that really ought to have a CHAR, you should fix it on the other end rather than making an integer array. TYPE_CAST() is made for just such situations as this.