以二进制格式打印字符串 [英] Print a string in binary format

查看:580
本文介绍了以二进制格式打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向Python专家致以问候,


我有一个二进制文件,希望看到原始文件它的内容。所以我在二进制模式下打开

,并一次读取一个字节到一个变量,

将是字符串类型。现在的问题是如何将该字符的二进制格式打印到标准输出。这似乎是一个常见的任务

但是我从文档中找不到合适的方法。

非常感谢。

解决方案

neutrino写道:

向Python大师致以问候,

我有一个二进制文件,希望看到原始它的内容。所以我以二进制模式打开它,并一次读取一个字节到一个变量,它将是字符串类型。现在问题是如何将该字符的二进制格式打印到标准输出。这似乎是一项常见的任务
但我无法从文档中找到合适的方法。
非常感谢。




这是怎么回事? *你想要什么:


import sys

f = open(filename,''rb'')

data = f。阅读(1)

数据:

sys.stdout.write(数据)

data = f.read(1)


当然,这是长版本:


print open(filename,''rb'')。read()


// m


嗯,我想要的输出格式如下:

0001110011111111000000001111111100000000 .. ..


我在Windows 2000下在Cygwin上尝试了你的代码,在Linux上,但它打印出ASCII字符。


中微子在2005年1月20日下午7:53发表以下内容:

向Python大师致以问候,

我有一个二进制文件,并希望看到原始 ;它的内容。所以我以二进制模式打开它,并一次读取一个字节到一个变量,它将是字符串类型。现在问题是如何将该字符的二进制格式打印到标准输出。这似乎是一个常见的任务
但我无法从文档中找到合适的方法。
非常感谢。




不是大师,但是我会尽力帮忙:-)


怎么样:

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


现在这就是telnet.hex的样子:

(这个文件必须用BinHex 4.0转换)


:#R4PE''jPG#jPH''8 !2J!)!!!!! 4B!N!@ 9 [deDN !!!!`!!!! 3 !!!

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.

解决方案

neutrino wrote:

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.



How is this *not* what you want:

import sys
f = open(filename, ''rb'')
data = f.read(1)
while data:
sys.stdout.write(data)
data = f.read(1)

Of course, that''s the long version of:

print open(filename, ''rb'').read()

// m


Mmm, the output format that I want is like:
0001110011111111000000001111111100000000....

I tried your code on Cygwin under Windows 2000, and on Linux, but it
prints out ASCII characters.


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!!!


这篇关于以二进制格式打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆