Reputation: 195
I would like to know what would be the best way to do the opposite of this post : encode Netbios name python
So to encode, you could use this :
encoded_name = ''.join([chr((ord(c)>>4) + ord('A'))
+ chr((ord(c)&0xF) + ord('A')) for c in original_name])
But to decode this for example :
Netbios_Name= "\x46\x45\x45\x46\x46\x44\x45\x45\x46\x45\x45\x45\x45\x45\x46\x44\x46\x44\x46\x44\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41"
## When correctly reversed, Netbios_Name should output in ASCII : "TESDTDDSSS"
I though about the reverse of this function, but i can't figure why it doesn't work.
Thanks !
Upvotes: 0
Views: 3750
Reputation: 9273
Use this function (taken from dpkt source code):
def decode_name(nbname):
"""Return the NetBIOS first-level decoded nbname."""
if len(nbname) != 32:
return nbname
l = []
for i in range(0, 32, 2):
l.append(chr(((ord(nbname[i]) - 0x41) << 4) |
((ord(nbname[i+1]) - 0x41) & 0xf)))
return ''.join(l).split('\x00', 1)[0]
So:
>> decode_name(Netbios_Name).strip()
'TESDTDDSSS'
Upvotes: 2