Netlinx string conversion (ASCII, integers)
maxifox
Posts: 209
I got some confusion when tried to clarify how the string stuff works in Netlinx. Exactly, what is relation between chars, integers, ASCII and so on. I believe it only looks simple but it is not indeed without getting used to it...
I have to implement a protocol for a switch that says (using Netlinx terminology):
" The command to make a switch is ?Ix:Oy? where x is the desired
input and y is the desired output. The input and output are ASCII
representations of the integer corresponding to input or output. "
What I do not fully understand is the term "ASCII representation of the integer". In computer we deal with bit sequences. Bit sequences do not imply any representation. ASCII defines (just an agreement) what symbol/glyph of a written language is represented by each 8 bits sequence.
111000(bit) represents ASCII char '8'. If you are not comfortable with bits you may say that 38(hex) or 56(dec) represent character '8' according to ASCII. That char '8' is different from integer 8 meaning you will find integer 56 if you look at raw data under '8'.
What exactly "ASCII representation of the integer 8" is for Netlinx? Does it mean ASCII character '8' - the result of itoa(8); or ASCII control character Backspace - result of char ch=8?
Getting back to my problem, for the first case Netlinx code should be be send_string dev, " 'I',itoa(x),':O',itoa(y) ". For second case - send_string dev, " 'I',x,':O',y ".
Any clarifications, please?
I have to implement a protocol for a switch that says (using Netlinx terminology):
" The command to make a switch is ?Ix:Oy? where x is the desired
input and y is the desired output. The input and output are ASCII
representations of the integer corresponding to input or output. "
What I do not fully understand is the term "ASCII representation of the integer". In computer we deal with bit sequences. Bit sequences do not imply any representation. ASCII defines (just an agreement) what symbol/glyph of a written language is represented by each 8 bits sequence.
111000(bit) represents ASCII char '8'. If you are not comfortable with bits you may say that 38(hex) or 56(dec) represent character '8' according to ASCII. That char '8' is different from integer 8 meaning you will find integer 56 if you look at raw data under '8'.
What exactly "ASCII representation of the integer 8" is for Netlinx? Does it mean ASCII character '8' - the result of itoa(8); or ASCII control character Backspace - result of char ch=8?
Getting back to my problem, for the first case Netlinx code should be be send_string dev, " 'I',itoa(x),':O',itoa(y) ". For second case - send_string dev, " 'I',x,':O',y ".
Any clarifications, please?
0
Comments
In netLinx, ITOA() converts any 32bit signed-integer Value to its ASCII representation:
ITOA(1234) results in a '1234' ASCII string
ITOA(-123) results in a '-123' ASCII string
The way back would be ATOI()
ATOI('23') results in a value 23
Differently to the manual, the ATOI() will also convert any 32bit signed-integer value: LONG Result = ATOI('-123') // result = -123
I hope this lhelps...
Excellect description of Integer to ASCII.
Most protocols use ASCII representations because that is what we type directly into the computer. If you are working in code with a numeric value, you always have to convert it to ASCII if that is what the protocol is asking for. In the Studio compiler, typing in a value within single quotes is the shortcut for the ASCII representation; if you are doing calculations, you have to run your result through a function like ITOA. Often, whe a protocol says they want a hex value, what they really mean is the ASCII representation of the hex number, not the hex number itself. If you are dealing with raw numbers, they all get converted to bits in the end, and it doesn't matter how you type them in - 10, $0A, and 1.0e1 are all the same thing in Studio. It's only when they need to be ASCII that you have to run them through any kind of conversion, because the ASCII representation is going to vary significantly in all these forms (which is plain just by how they are typed, though they are all the same numeeric value). YOu could just enclose those numbers in single quotes to "convert" them to ASCII for the compiler, but if you are dealing with the contents of a variable, or a calculated value, you have to use ITOA or ITOHEX, whichever is appropriate.
Thank you, DHawthorne for clarification. Looks I thought of it too much and finally get lost... Hmmm, "Lost in Translation" I would say...
But yeah, usually the request for an "ASCII '8'" translates to "The character at index 56 of the ASCII chart" - even though your code is sending "56" (or "$38") out the port.
- Chip
http://www.lookuptables.com/ is a handy website which shows you the table of basic ASCII characters. Notice the first 32 ASCII codes actually don't refer to text characters at all, but actions.