tags:

views:

24

answers:

2

Hi Everyones,

I'm trying to create a simple script that will convert a string (max 15 chars) to a netbios name (see http://support.microsoft.com/kb/194203) :

name = sys.argv[1].upper()
converted = ''.join([chr((ord(c)>>4) + ord('A'))+chr((ord(c)&0xF) + ord('A')) for c in name])
print converted

Trying to convert the name : "testing" will return : "4645454646444645454a454f4548" which is correct. Now depending the length of the name submitted (max 15 chars) we need to pad 4341 until the converted string is 64 long. Example :

./script.py testing:
4645454646444645454a454f4548

But should actually be : 4645454646444645454a454f4548434143414341434143414341434143414341

Anyways to do this easily ?

Thanks !

A: 

I am sure there are better ways to do it, but here is one you can try:

name = "4645454646444645454a454f4548"
pad = "4341"
whole, remainder = divmod(64 - len(name), 4)
name = "%s%s%s" %(name, whole * pad, pad[:remainder])
print name

OUTPUT

4645454646444645454a454f4548434143414341434143414341434143414341
sberry2A
A: 
... + ((16 - len(name)) * '4341')
Ignacio Vazquez-Abrams