Home AMX User Forum NetLinx Studio

ASCII to HEX

NeedToKnowNeedToKnow Posts: 3
edited May 2023 in NetLinx Studio

Hey there brains trust. I've have been out of the AMX programming game for a while, but I am just starting to dip my toe in again. I am writing a little module that allows the user to save a number of MAC addresses (input from the touch panel as a character string: XX:XX:XX:XX:XX:XX, saved in a character array ) I have this portion working well, however I am struggling with the conversion of the ASCII representation of the HEX values to real HEX values. This is so I can construct a magic packet for Wake On Lan for each of the MAC addresses. String manipulation has always been a week point of mine. Any help or advice, or even sample code would be fantastic. ChatGPT doesn't do Netlinx very well...

Comments

  • John NagyJohn Nagy Posts: 1,742
    edited May 2023

    Consider making a lookup table in an array.

  • basically, this would be a job for HEXTOI() (hex to integer).
    HEXTOI interprets an ASCII row containing 0..9,A..F/a..f as a hex representation, and converts it to a numeric equivalent.


    integer nNumericValue
    char sHexinAscii[8]

    sHexInAscii = 'FE'

    nNumericValue = ITOHEX(sHexInAscii) // converts the string 'FE' that represents a Hex value, into numeric 254 / $FE.


    Don't get confused by the "I" of HEXTOI - there is no difference in value noting $FE or 254, the "integer" means "numeric"

    In case of an ascii string '00:60:9f:11:22:33' , itohex() will begin to interpret the '00', but stops at the first ':', because the ':' is no hex-like character. So you may have to do some kind of loop to parse the 6 octets.

    Might be easier to do a lookup table as John said, and compare the full MAC address string.

  • richardhermanrichardherman Posts: 401

    You could do something like this:

    DEFINE_FUNCTION CHAR[6] fnMACAddressConversion(CHAR cMACAddress[])
    {
        STACK_VAR INTEGER i
        STACK_VAR CHAR cMAC[6]
        STACK_VAR CHAR cMACReturn[6]
    
        IF (LENGTH_ARRAY(cMACAddress) == 17) //very crude check
        {
        For(i = 1; i <= 6; i++)
        {
            cMAC[i] = HEXTOI(GET_BUFFER_STRING(cMACAddress, 3)) //: will be ignored
        }
        cMACReturn = "cMAC[1],cMAC[2],cMAC[3],cMAC[4],cMAC[5],cMAC[6]"
        SET_LENGTH_ARRAY(cMACReturn, 6)
        RETURN cMACReturn
        }
    }
    

    If you call the function with a MAC address as an ASCII string, it will return that address as a numeric array:

    cMACAddr = fnMACAddressConversion('80:EE:F3:DE:1C:95')

    I've tried it very briefly and it does seem to work.

    • The function does nothing to verify that what you feed it is actually a MAC address and if not will probably fail unpleasant... But I gather you seem to have control over the input, so maybe more checks won't be necessary.

    • If your MAC address contains a numeric zero (common case) and you try to display that in the diagnostics windows, you won't see the string from the point on where the 0 is encountered. So if the address starts with a 0 the result seems empty... it isn't, that's just an annoying many, many, year old display bug in the diagnostics window.

Sign In or Register to comment.