Home AMX User Forum NetLinx Studio
Options

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

  • Options
    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.....
  • Options
    yuriyuri Posts: 861
    ITOA(HEXTOI(strHexString))

    ;)
  • Options
    travtrav Posts: 188
    Or if you are getting it in as comma seperated hex values, just bung it in a char array.
  • Options
    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' "
  • Options
    mpullinmpullin Posts: 949
    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.
  • Options
    yuriyuri Posts: 861
    i will post three ;) next time to denote the amount of sarcasm :p
  • Options
    mpullinmpullin Posts: 949
    yuri wrote:
    i will post three ;) next time to denote the amount of sarcasm :p
    umm... lol? :p
  • Options
    TurnipTruckTurnipTruck Posts: 1,485
    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.
  • Options
    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)
  • Options
    TurnipTruckTurnipTruck Posts: 1,485
    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.
  • Options
    DHawthorneDHawthorne Posts: 4,584
    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.
Sign In or Register to comment.