Home AMX User Forum AMX General Discussion

HEX/ASCII/DECIMAL Conversion

TonyAngeloTonyAngelo Code MonkeyPosts: 315
I'm trying to figure out how to parse a string I'm getting back from a DirecTV HR20. It's a response string from a current channel request and there is sometimes an ASCII character mixed in with the hex code.

Here's an example (channel 72):

$F0 $00 H $FF $FF $F4

The important stuff is the $00 H

The $00 is the number of 256's I need to add the the next character to get the correct channel. The H is 72. The problem I'm having is converting the H into something I can see and send to the panel.

TIA!

Comments

  • NMarkRobertsNMarkRoberts Junior Member Posts: 455
    Tony, you aren't the first and you won't be the last to need some help in this area. It's actually much simpler than it looks.

    Here is some code to do the job. This is compiled but not tested:
    program_name = 'Fred'
    
    define_function char[100] ConvertChannel(
      char sArgReply[])
    {
    stack_var integer nMyByte1
    stack_var integer nMyByte2
    stack_var integer nMyResult
    stack_var char sMyResult[100]
    
    nMyByte1 = type_cast(mid_string(sArgReply,2,1))
    nMyByte1 = type_cast(mid_string(sArgReply,3,1))
    
    nMyResult = (nMyByte1 * 256) + nMyByte2
    
    sMyResult = itoa(nMyResult)
    
    return sMyResult;
    }
    

    I tend to code NetLinx very defensively.

    I've used mid_string(sArgReply,2,1) instead of sArgReply[2] because the latter can behave very oddly. This forces me to use type_cast but that's not a bad thing as it illustrates what's happening. There's a semicolon on the return statement because the return statement (uniquely of all keywords) needs one sometimes.

    Here is the same code squashed onto one line and not quite so industrial strength, once again not tested:
    define_function char[100] ConvertChannel(
      char sArgReply[])
    {
    return itoa((sArgReply[2] * 256) + sArgReply[3])
    }
    
  • my thought is, ignore that byte 3 reveals itself as Ascii, just relate to it as an integer.

    example:

    integer aNumber
    integer offset = 3
    if(offset>0)
    aNumber = type_cast(IncomingString[offset])


    also, you can use the FORMAT command to convert numbers back into a string.

    example:

    OutgoingString = "Format('%02d ',Number1),Format('%02d ',Number2),Format('%02d ',Number3),"

    or

    OutgoingString = Format('%02x ',type_cast(IncomingString[offset]))

    just some suggestions for you. not sure if it's what you need though.
  • jweatherjweather Junior Member Posts: 320
    See the above replies for actually parsing it, but I'll just mention that you should ignore whether a character shows up as "ascii" or "hex" in the notifications -- you can treat everything as hex, or everything as ascii (though not necessarily printable ascii!). NetLinx Studio tries to make an educated guess about whether to display a given character as a hex code or an ASCII character, and it's frequently wrong in confusing ways when it starts mixing the two. You can think of that H as being identical to $48, which is the (decimal) 72 you're looking for. The way it's displayed doesn't affect the underlying value -- it's still the same 8 binary bits.

    Jeremy
  • John GonzalesJohn Gonzales Junior Member Posts: 609
    NetLinx Studio tries to make an educated guess about whether to display a given character as a hex code or an ASCII character, and it's frequently wrong in confusing ways when it starts mixing the two.

    Jeremy's right about treating everything as either hex or ascii in the notification strings. I don't think it's quite so much NS2 making an educated guess though... In diagnostics and notifications, if the the character is unprintable it gets displayed as either a hex or decimal value. If it's a printable ASCII character then it gets displayed as the ASCII equivalent. Under the preferences menu in NS2 you can select whether to display the unprintable characters as either hex or decimal. Also, another annoying thing is that in the Diagnostics tab, if a null character ($00) comes across, anything after the null is not displayed.

    Here's an excerpt from the help file regarding the options under the "diagnostics" tab setting in the Preferences menu:

    " Display Control Codes as ? Converts unprintable characters (i.e. Control Codes) into either Hexadecimal or Decimal values within the message (default = Hex Values)."

    --John
  • jweatherjweather Junior Member Posts: 320
    I don't think it's quite so much NS2 making an educated guess though...

    I didn't say it was well-educated. ;) They would probably need a DWIM (Do What I Mean) button to do any better than just printable vs. unprintable. It seems to me that a string with more than 50% unprintable characters should probably be printed in all hex... but at least the current way is completely deterministic, and won't change the entire display format just because one byte changed values!
    Under the preferences menu in NS2 you can select whether to display the unprintable characters as either hex or decimal.

    Now they just need to add an option to show everything as hex. Of course, if you're doing more than just a little bit of hacking on a device, you can just dump out the strings as debug messages in hex for viewing.

    Jeremy
  • TonyAngeloTonyAngelo Code Monkey Posts: 315
    Here is some code to do the job. This is compiled but not tested:

    Worked like a charm. I kept trying to use HEXTOI thinking I had to convert the string.

    Thanks for your help.
Sign In or Register to comment.