ASCII to HEX string
HARMAN_m_hodel
Posts: 12
Hi All, I'm trying to convert an incoming ASCII string to a HEX value. The incoming string contains an ASCII volume level such as '35' which is really the HEX value of the actual volume state of the device. I don't have a problem isolating the number from the incoming string, but I haven't found a way to convert it to a NetLinx HEX value. Any help would be appreciated.
Sample incoming string-- '!1VOL35'
Marc
Sample incoming string-- '!1VOL35'
Marc
0
Comments
strCmd = REMOVE_STRING(DATA.TEXT,'VOL ',1)
strInfo = DATA.TEXT
if(strCmd && (length_string(strCmd) > 0 ))
{
nBigByte = GET_BUFFER_CHAR(strInfo) // in your sample it will be 3
nSmallByte = GET_BUFFER_CHAR(strInfo) // in your sample it will be 5
nVolume = ATOI("nBigByte,nSmallByte")
}
Using get_buffer-char to load the integer variable nBigByte or nSmallByte will likely cause a compiler warning w/o the use of TYPE_CAST or what ever that system function is.
The end result is that you just coverted the ascii string value to an interger and not the hex value. Totally different result.
INTEGER nBigByte
INTEGER nSmallByte
INTEGER nVolume
CHAR strCmd[30]
CHAR strInfo[100]
And from what I see he want the decimal value of the volume...
If he needs it in HEX he can convert it to hex with ITOHEX
Is just one more way to do it, thats all
Two things:
you are relying on the device sending exactly one complete string. If the device sends its strings out so that data.text is very likely to contain a complete string and nothing more, this is often ok. Some devices don't do this, however. You may find data.text to occasionally contain more than one string and sometimes an incomplete string. The "while" in VAVs example accounts for this (assuming that strings from the device have a standard ending character).
The ASCII/hex representation in the string may be more than two bytes. and is likely if the possible volumes are from 0 through 255.
If you are certain that data.text will contain exactly one complete string, you can put this all in one line:
The ATOI function will ignore ascii characters that don't make sense as integers.
Vav solution is better and cover more scenarios.
good to know this
"The ATOI function will ignore ascii characters that don't make sense as integers"
Just in case any one tries to use this I added and else statement to eliminate creating an infinite while loop should "13" end up in the begining of the buffer. In this example I added the "13" delimiter which the device may not actually provide.
with vining, here is a sample copied out of a working Integra code block..
DATA_EVENT[dvFAMILY_RCVR]
{
STRING:
{
cFAM_RCVR_BUF = "cFAM_RCVR_BUF,DATA.TEXT"
WHILE(FIND_STRING(cFAM_RCVR_BUF,"$1A",1))
{
cFAM_RCVR_DATA = REMOVE_sTRING(cFAM_RCVR_BUF,"$1A",1)
IF(FIND_STRING(cFAM_RCVR_DATA,"'!1PWR'",1))
{
IF(FIND_STRING(cFAM_RCVR_DATA,"'00'",1))
{
nFAM_RCVR_POWER = 0
}
ELSE IF(FIND_STRING(cFAM_RCVR_DATA,"'01'",1))
{
nFAM_RCVR_POWER = 1
}
}
ELSE IF(FIND_STRING(cFAM_RCVR_DATA,"'!1MVL'",1))
{
REMOVE_STRING(cFAM_RCVR_DATA,"'MVL'",1)
nFAM_RCVR_VOL = HEXTOI(LEFT_STRING(cFAM_RCVR_DATA,2))
}
}
}
}
True. the function to use in this case is HEXTOI, and not ATOI.
Thanks to everyone for the help on this- I didn't get a chance to reply earlier, but your quick responses helped me work this out.
Marc