Home AMX User Forum AMX General Discussion

Netlinx string conversion (ASCII, integers)

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?

Comments

  • The SEND_STRING is
    SEND_STRING dvDev,"'I',ITOA(x),':O',ITOA(y)"
    

    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...
  • Well done

    Excellect description of Integer to ASCII.
  • DHawthorneDHawthorne Posts: 4,584
    The difference between a CHAR and an INTEGER is simply the size of the data allocation. A CHAR is 8 bits (0-255), and an INTEGER is 16 bits (0-65536). In some programming languages, they are the same, but this is the allocation in NetLinx. ASCII is exactly what you described, and always what you described: an 8-bit (CHAR) representation of a glyph; in the case of a numeral, the glyph for that numeral with no other relation to the numeric value of the represented number. Strings in NetLinx are simply CHAR arrays.

    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.
  • maxifoxmaxifox Posts: 209
    Lost in Translation

    Thank you, DHawthorne for clarification. Looks I thought of it too much and finally get lost... Hmmm, "Lost in Translation" I would say...
  • Don't know if this helps any further, but you can also keep in mind that ASCII is nothing more than a long time and widely known TABLE of characters. The art is in knowing when to translate between (often Jinglish) descriptions of protocols and what the device actually expects. :)

    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
  • mpullinmpullin Posts: 949
    Chip Moody wrote:
    Don't know if this helps any further, but you can also keep in mind that ASCII is nothing more than a long time and widely known TABLE of characters.

    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.
  • Joe HebertJoe Hebert Posts: 2,159
    And you can also view a table in the Netlinx Studio help file. Table of Contents ---> NetLinx Programming Overview --> ASCII /HEX / Decimal Conversions Table.
Sign In or Register to comment.