Home AMX User Forum NetLinx Studio

Hex to bin

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.

Comments

  • DHawthorneDHawthorne Posts: 4,584
    That depends on what you are really asking. Do you want binary to go to the device, or an ASCII representation of the binary?

    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.
  • TurnipTruckTurnipTruck Posts: 1,485
    If someone actually wanted an ASCII representation of the binary bits, is there a way to do that? I can't think of one.
  • TurnipTruckTurnipTruck Posts: 1,485
    DHawthorne wrote: »
    they want the ASCII representation of the hex

    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.
  • LehaLeha Posts: 37
    hex???

    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?
  • PhreaKPhreaK Posts: 966
    If someone actually wanted an ASCII representation of the binary bits, is there a way to do that? I can't think of one.
    /**
     * Convert a single byte into an ASCII representation of its data (LSB 0).
     *
     * @param	x		a single byte to translate
     * @return			an ASCII representation of the data contained in x
     */
    define_function char[8] byte_to_binary(char x)
    {
    	char ret[8]
    	char bit
    	
    	for (bit = 7; bit >= 0; bit--) {
    		ret[8 - bit] = (x & 1 << bit) + $30
    	}
    	
    	return ret
    }
    

    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.
  • DHawthorneDHawthorne Posts: 4,584
    Leha wrote: »
    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?

    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.
  • Find attached an include file with some math operations (rename from axs to axi). It has a function INT2BIN() to convert a numeric value into an ASCII string representing the bits.
    DEFINE_VARIABLE 
    VOLATILE CHAR sMyBits[8]
    ...
    // CHAR sResult = INT2BIN(<value>,<amount of digits in result>)
    sMyBits = INT2BIN($FE,8) // result is sMyBits = '11111110'
    

    This is a non official Include, so use at your own risk.
Sign In or Register to comment.