Hex to bin
Leha
Posts: 37
How to translate decimal number in the binary? For example 4 in 00000100, and 87 in 01010111. It is necessary that 01010111 was in the string.
0
Comments
As far as device level communication is concerned, there is no need to translate at all. Everything goes out and comes in in binary. The only time it is needed to translate is for humans to read it. Sending "'Hi!'", or "$48,$69,$21", or "72,105,33" (leaving out the binary notation from sheer laziness) are all identical to the master and the devices, it's only the way you read them that differs.
Some protocols, and I must say, they irritate me greatly, require an ASCII representation of a command, but they are very rare. Typically, they don't want it in binary either, they want the ASCII representation of the hex. In that case, you just enclose your string in single quotes, using whatever notation is required. I might add, most use a notation like '0x48' for hex, or '48h', rather than the '$48' NetLinx uses. the ITOHEX() function convert more complex numbers to an ASCII string, but there is no ITOBIN() equivalent. But, this is very important, this does not convert your number to hex, it converts it to an ASCII, human readable, string showing the hex notation. If you send this to your device, and it doesn't specifically call for an ASCII representation, it simply will not work.
I always thought that was silly also. However, it has grown on me as it can make debugging easier. I've done a lot of work with the UPB lighting control system. Its commands are literal hex. The FORMAT command does a very easy integer-to-literal hex conversion.
I receive the answer from illumination system. If I do so:
myhex=DATA.TEXT [7]
mystring=ITOHEX (myhex)
If myhex=10 or 11 or 12... FF at me in mystring two symbols, and if from 00 to 0F, only one!
1=ITOHEX (01) or 2=ITOHEX (02)... And it is necessary for me 01=itoxeh (01)
Further I compare if (myhex [1] = ' 0 ') or if (myhex [2] = ' 2 ')
How it is possible to receive always two symbols for the further comparison?
That should do it (it's untested though). If you need to convert a string iterate for each character and concatenated the results, for other data types convert to a string first using RAW_BE/RAW_LE and iterate.
By using the [7] on DATA.TEXT, you are telling the program you only want the 7th character in the string. By definition, a CHAR variable in NetLinx cannot be bigger than FF. If you need more than one character, you will have to use one of the string functions, like MID_STRING(DATA.TEXT, 7, 2), or something like that, depending on what you want, and whether position 7 in your DATA.TEXT is always where what you are looking for is going to be.
I'm still not convinced you really need ITOHEX. Remember, it does not convert a value to a hex value. It converts a value to an ASCII string in hex notation. It's exactly like ITOA, where you need to convert a number to an ASCII string in decimal notation.
This is a non official Include, so use at your own risk.