<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>开心e点 &#187; python</title>
	<atom:link href="http://www.ebnd.cn/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ebnd.cn</link>
	<description>好好学习 天天向上</description>
	<lastBuildDate>Tue, 21 Jun 2011 15:00:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>python 下读取二进制文件</title>
		<link>http://www.ebnd.cn/2009/08/29/parse-binary-file-in-python/</link>
		<comments>http://www.ebnd.cn/2009/08/29/parse-binary-file-in-python/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 22:00:31 +0000</pubDate>
		<dc:creator>dandan</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.ebnd.cn/?p=16</guid>
		<description><![CDATA[在解析一个二进制文件的时候，想把读取的单个的byte转换为python的char类型值 f = file&#40;...&#41; buf = f.read&#40;1024&#41; int&#40;buf&#91;0&#93;&#41; 结果总是报错：ValueError: invalid literal for int() with base 10: &#8216;\x90&#8242; 这里找到了对这个问题的解释和解决的办法： 在python中，file.read 得到的结果是string, 要转换成别的类型，需要用到struct。 import struct ... value = struct.unpack&#40;fmt, buf&#91;0&#93;&#41;&#91;0&#93; 关于 struct.unpack 的用法，可以参考这个链接，其中有 fmt 的详细的说明。 举例来讲，如果是想转换成unsigned char，就用： uchar = struct.unpack&#40;'B', buf&#91;0&#93;&#41;&#91;0&#93; 相关文章无相关文章]]></description>
			<content:encoded><![CDATA[<p>在解析一个二进制文件的时候，想把读取的单个的byte转换为python的char类型值</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">f = <span style="color: #008000;">file</span><span style="color: black;">&#40;</span>...<span style="color: black;">&#41;</span>
buf = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1024</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>buf<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>结果总是报错：ValueError: invalid literal for int() with base 10: &#8216;\x90&#8242;</p>
<p><br class="spacer_" /></p>
<p>这里找到了<a title="http://stackoverflow.com/questions/1163459/reading-integers-from-binary-file-in-python" href="http://stackoverflow.com/questions/1163459/reading-integers-from-binary-file-in-python">对这个问题的解释和解决的办法</a>：</p>
<p><br class="spacer_" /></p>
<p>在python中，file.read 得到的结果是string, 要转换成别的类型，需要用到struct。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">struct</span>
...
<span style="color: black;">value</span> = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span>fmt, buf<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>

<p>关于 struct.unpack 的用法，可以参考<a href="http://docs.python.org/library/struct.html">这个链接</a>，其中有 fmt 的详细的说明。</p>
<p><br class="spacer_" /></p>
<p>举例来讲，如果是想转换成unsigned char，就用：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">uchar = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'B'</span>, buf<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>

<p><br class="spacer_" /></p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>无相关文章</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ebnd.cn/2009/08/29/parse-binary-file-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

