Print a string in binary format

Kartic removethis.kartic.krishnamurthy at gmail.com
Thu Jan 20 20:34:17 EST 2005


neutrino said the following on 1/20/2005 7:53 PM:
> Greetings to the Python gurus,
> 
> I have a binary file and wish to see the "raw" content of it. So I open
> it in binary mode, and read one byte at a time to a variable, which
> will be of the string type. Now the problem is how to print the binary
> format of that charater to the standard output. It seems a common task
> but I just cannot find the appropriate method from the documentation.
> Thanks a lot.

Not a guru, but I will try to help out :-)

how about this:
 >>> import binhex
 >>> binhex.binhex('C:/windows/system32/telnet.exe', 'C:/TEMP/telnet.hex')
 >>>

Now this is what telnet.hex looks like:
(This file must be converted with BinHex 4.0)

:#R4PE'jPG#jPH'8!2j!)!!!!!4B!N!@9[deDN!!!!`!!!!3!!!$rr`!!Z!#3"d!
!N#2J!!!!$Kqk$J#d#FdKZ!&-c5&8D'Pc)("bEfGbB at dJBf&ZEQpd)'*P)(*eEL"
TEL"%6e-JE at pNC5i0$3SN!*!(Q$X`mpaDAU$F at PkJh&THS#Cj(U$G at PkJh&TIS'P
DAU!QH8HJceTHS%Yj'k$G at PkJ"RP#S-TDAU!'H81Jh9THS#CjBk$G at PkJ8QPMD0a
DAU!!N""343!!6!%$!&VGE6d!N!MJ!!m"#`%(!!$!!!!!"J)!N!AQ[3!!!"!!!!$
3!*!&!3!3!!!!!J!!"3!"!!8!!3!%!*!)i!)!!!3!!&Hl!3!$!!#!!!!%!!!3!*!
%%!!!%!#3"K!!N!Z3!-%!!-J!N!5J!J"B1!#3'[!5!!!F!*!M8!)!!-`!N!33!!$
F!J#3'LjdCAKd!!!!rVm!!!!3!!!!`!!!!!3!N!iJ!!"J,Q4KG'%!!!"mb`%!!0!

(... Big Snip ...)


Or how about this?

 >>> f = open('C:/windows/system32/telnet.exe', 'rb')
 >>> fcontents = f.read()
 >>> import binhex
 >>> print binhex.binascii.hexlify(fcontents[0:10])
'4d5a9000030000000400'
 >>>

Is this what you want???

Thanks,
--Kartic




More information about the Python-list mailing list