Announcement

Collapse
No announcement yet.

Address Conversion to Hex

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Address Conversion to Hex

    Does anyone have a function that will convert an address in the form "23:4D:3E" (a string) to the form required for the rpc method?

    n = int(mac.replace(':', ''), 16) doesn't seem to work as all the functions are not allowed.

    I have also tried something like:
    tmpAddrStr = AddrStr[0:1] & AddrStr[3:4] & AddrStr[6:7]
    tmpConvertedHexAddr = int(tmpAddrStr, 16)
    rpc(tmpConvertedHexAddr,"waitForData","Hello")

    This results in an Allocation Failed in Function stdInEvent....

    I've also tried:
    tmpAddrStr = chr(int(tmpAddrStr[0:1],16)) + chr(int(tmpAddrStr[3:4],16)) + chr(int(tmpAddrStr[6:7],16))

    I would think that should work... there are no errors, but using the resulting address does not appear to call the remote synapse module.

    Any help would be appreciated.

    Thank you.
    Last edited by teamsp; 06-11-2015, 04:22 PM.

  • #2
    Anybody have any thoughts on this one?

    Comment


    • #3
      I would suggest seacrhing the forum for hex support also the attached files should help
      Attached Files

      Comment


      • #4
        Thank you.

        Comment


        • #5
          The issue is slicing

          If I understand your problem: using slice [3:4] for example, will only return the byte/character at location 3. You have to use [3:5] to get elements 3 and 4. I got bit by this myself until I got Python slicing down.

          Comment


          • #6
            I use these three methods I can't remember where I found them send AABBCC to convStrAddr(addrStr)

            I have the methods to go the other way also if you need them

            SORRY ABOUT THE SPACING I CAN'T SEEM TO PASTE THE METHODS IN WITH CORRECT INDENTATION



            def convStrToAddr(addrStr):
            '''Convert a regular string into a SNAP address
            Expects 'AABBCC' address format as input'''
            if len(addrStr) >= 6:
            byte1 = addrStr[0:2]
            byte2 = addrStr[2:4]
            byte3 = addrStr[4:6]
            return chr(convStrToByte(byte1)) + chr(convStrToByte(byte2)) + chr(convStrToByte(byte3))


            def convStrToByte(byteStr):
            '''Convert a byte-sized string'''
            msd = byteStr[0]
            lsd = byteStr[1]
            result = convStrNibble(msd) << 4
            result += convStrNibble(lsd)
            return result

            def convStrNibble(nibble):
            '''Convert the nibble to hex value'''
            numStr = "0123456789"
            i = 0
            found = False
            while i < len(numStr):
            if nibble == numStr[i]:
            found = True
            break
            i+=1
            if found:
            return (ord(nibble) - ord('0'))

            capStr = "ABCDEF"
            i = 0
            found = False
            while i < len(capStr):
            if nibble == capStr[i]:
            found = True
            break
            i+=1
            if found:
            return (ord(nibble) - ord('A') + 10)

            lwrStr = "abcdef"
            i = 0
            found = False
            while i < len(lwrStr):
            if nibble == lwrStr[i]:
            found = True
            break
            i+=1
            if found:
            return (ord(nibble) - ord('a') + 10)

            return None

            Comment

            Working...
            X