HEX/ASCII/DECIMAL Conversion
TonyAngelo
Posts: 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!
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!
0
Comments
Here is some code to do the job. This is compiled but not tested:
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:
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.
Jeremy
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
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!
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
Worked like a charm. I kept trying to use HEXTOI thinking I had to convert the string.
Thanks for your help.