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))
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:
else:
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))