PDA

View Full Version : Extended Hex Support


Jheath
11-24-2009, 05:08 PM
Here's a little ditty that allows you to covert SNAP Addresses to/from regular strings.

One application would be a command line interface to the UART that would need to be able to read and write RPCs to specific addresses.

Use the convBinToStr() to convert an address to a string (in the format AABBCC)
Use convStrToAddr() to convert a string ("AABBCC") to a SNAP address.



def hexNibble(nibble):
'''Convert a numeric nibble 0x0-0xF to its ASCII string representation "0"-"F"'''
hexStr = "0123456789ABCDEF"
return hexStr[nibble & 0xF]

def printHex(byte):
'''print a byte in hex - input is an integer, not a string'''
print hexNibble(byte >> 4),
print hexNibble(byte), # no trailing CR/LF

def byteToHex(byte):
'''print a byte in hex - input is an integer, not a string'''
upper = hexNibble(byte >> 4)
lower = hexNibble(byte)
return upper + lower

def printHexWord(word):
'''print a word in hex - input is an integer, not a string'''
printHex(word >> 8)
printHex(word & 0xff)

def dumpHex(str):
'''dump a string of bytes in hex'''
count = len(str)
index = 0
while index < count:
printHex(ord(str[index]))
index += 1
if (index & 15) == 0:
print
else:
print ' ',
print

def convBinToStr(str):
'''Convert a string of bytes in hex
Returns "AABBCC"'''
count = len(str)
index = 0
bigStr = ''
while index < count:
bigStr += byteToHex(ord(str[index]))
index += 1
return bigStr

#----------------------------- Reverse Direction ------------------------
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

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

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]
print 1
return chr(convStrToByte(byte1)) + chr(convStrToByte(byte2)) + chr(convStrToByte(byte3))

bcuming
02-26-2010, 09:30 AM
I need to convert the localAddr to hex but was not able to get these to work on the RFE as bytetohex isn't supported. Am I missing a library or is this for Portal only?? I am not using Portal and need to do this in the RFE. What do you recommend?

kbanks
02-26-2010, 05:21 PM
I don't understand the question.

Routine byteToHex() is *IN* the example script Jonathan posted.

???

bcuming
02-26-2010, 05:35 PM
Got it - working now - thanks

IbarraE
10-29-2010, 03:40 PM
Here is another example of converting an ASCII string to a binary string


def ChartoHex(char):
if char == 'A' or char == 'a':
return 0x0a
elif char == 'B' or char == 'b':
return 0x0b
elif char == 'C' or char == 'c':
return 0x0c
elif char == 'D' or char == 'd':
return 0x0d
elif char == 'E' or char == 'e':
return 0x0e
elif char == 'F' or char == 'f':
return 0x0f
elif ord(char) > 47 and ord(char) < 58:
return ord(char)-48
else:
return 0

def ASCIItoHex(string):
strlen = len(string)/2
newStr = ''
i = 0
while i < strlen:
newVal = ChartoHex(string[i*2])<<4
newVal += ChartoHex(string[i*2+1])
newStr += chr(newVal)
i += 1
return newStr