[Tutor] question about ascii and bytes

Alan Gauld alan.gauld at freenet.co.uk
Mon Feb 6 23:26:51 CET 2006


> the server wants to get info a certain way. Each message has to start
> with an ascii "STX" (literally the three letters, not the standart ascii
> 'STX') then it has to have another four bytes that give the length of
> the message, then the message itself, then end with an ascii "ENX" 
> (again,the three letters, not the ascii character "ETX" ).
> 
> So here is where the biggest gap in my understanding is... how do i send
> ascii characters ? 

You can just send the string of characters or you could use the struct 
module to compose the byte string with more control. But in this case 
a simple string should suffice.

> the docs say that if the password is not 24 bytes long, the other bytes
> must be filled with null values. How do i do that ? What is a null
> value?

Check an ASCII table but I beliebe ASCII NULL is zero, so 

null = chr(0)

should get you a null character.

Or even use escape characters:

null = '\x00

or even

null = '\0'

Then pad your string with

target = 24
mystring = 'spam'
padding = null * (target - len(mystring)-6)

passwd = 'STX' + mystring + padding + 'ENX'

> python strings are ascii already , right ? would this be as simple as
> simply passing the string to the socket.send('pythonAsciiString') ?

Yep, or you could specify UTF8 if you want to protect against any risk 
of Python becoming 16bit Unicode by default! :-)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list