2

I would like to encode "ITSATEST" to it's netbios name value in python; The occurence table and explication are here: http://support.microsoft.com/kb/194203

I dont know how this could be done easily in python, someone can give me a hand ?

Thanks !

helpmenet
  • 23
  • 3

2 Answers2

2

You can map each nibble of the original string, taking its numerical value and offsetting from 'A':

encoded_name = ''.join([chr((ord(c)>>4) + ord('A'))
                        + chr((ord(c)&0xF) + ord('A')) for c in original_name])
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • @joshperry, I presented the first one because it was easy to understand (and easy to write up while my brain was working on the second). Given your and @helpmenet's support of the "smarter" second solution, I've removed it. – Blair Conrad Dec 27 '09 at 13:25
1

Take a look at RFC 1001, which defines the encoding. In section 14.1 "FIRST LEVEL ENCODING" is the algorithm for the encoding, which you could implement directly in Python.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190