是否有标准的方法来写8位字节? [英] Is there a standard way to write an 8 bit byte?

查看:94
本文介绍了是否有标准的方法来写8位字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在写一个简单的图像文件导出器作为学校项目的一部分,

我想写完全平台 - 独立代码,如果有的话,可能是
。我遇到的问题是,显然没有

保证特定实现的数据类型为

正好是8位宽; char只能保证至少8位。

纠正我,如果我错了,但我认为如果CHAR_BIT恰好是,

比如,9,在二进制模式下将char写入文件将导致9位

被写入。


起初看起来stdint.h和它的uint8_t是回答,但是

然后我注意到uint8_t只保证存在如果平台

的数据类型正好是8位宽,这让我回到正方形

一个。


有没有解决方法,或者我只需要使用uint8_t和

接受我的代码可能

有一个ANSI C编译器吗?

Kristian

Hi,

I''m writing a simple image file exporter as part of a school project,
and I would like to write completely platform-independent code if at all
possible. The problem I''ve run into is that apparently there''s no
guarantee that a particular implementation will have a data type that is
exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct me if I''m wrong, but I assume that if CHAR_BIT happens to be,
say, 9, writing a char into a file in binary mode would result in 9 bits
being written.

At first it seemed that stdint.h and its uint8_t were the answer, but
then I noticed that uint8_t is only guaranteed to exist if the platform
has a data type that is exactly 8 bits wide, which put me back in square
one.

Is there a way around this, or will I just have to use uint8_t and
accept that my code might not work on every conceivable platform that
has an ANSI C compiler?
Kristian

推荐答案

文章< ZW *************** @ reader1.news.jippii.net>,

Kristian Nybo< kr ****** @ begone.spam.iki.fi>写道:
In article <ZW***************@reader1.news.jippii.net>,
Kristian Nybo <kr******@begone.spam.iki.fi> wrote:
我正在写一个简单的图像文件导出器作为学校项目的一部分,
我想写完全独立于平台的代码,如果可能的话。


PBM - 便携式BitMap(单色)

PNG - 便携式网络图形(彩色,无损压缩)

问题我遇到的是,显然没有
保证特定实现的数据类型恰好是8位宽; char只能保证至少为8位。
如果我错了,请纠正我,但我认为如果CHAR_BIT恰好是,请说,9,将char写入二进制文件模式将导致写入9位。
I''m writing a simple image file exporter as part of a school project,
and I would like to write completely platform-independent code if at all
possible.
PBM -- Portable BitMap (monochrome)
PNG -- Portable Network Graphics (colour, lossless compression)
The problem I''ve run into is that apparently there''s no
guarantee that a particular implementation will have a data type that is
exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct me if I''m wrong, but I assume that if CHAR_BIT happens to be,
say, 9, writing a char into a file in binary mode would result in 9 bits
being written.




是 - 但是当您将该文件带到8位机器时会发生什么情况

未定义。很少有文件系统(没有我知道的)存储文件作为比特流
:它们是字节或字流。以太网是比特流,

但是大多数众所周知的文件传输协议都涉及8位字符。

-

Ceci,ce n' 'est pasuneidée。



Yes -- but what happens when you take that file to an 8-bit machine
is undefined. Very few filesystems (none I -know- of) store files
as bitstreams: they are byte or word streams. Ethernet is bitstream,
but most well-known file-transfer protocols involve 8-bit characters.
--
Ceci, ce n''est pas une idée.




" Kristian Nybo" < KR ****** @ begone.spam.iki.fi>写了

"Kristian Nybo" <kr******@begone.spam.iki.fi> wrote
我正在写一个简单的图像文件导出器作为学校项目的一部分,并且
我想写完全独立于平台的代码,如果可能的话。我遇到的问题是,显然没有
保证特定实现的数据类型恰好是8位宽; char只能保证至少为8位。
如果我错了,请纠正我,但我认为如果CHAR_BIT碰巧是,那么,将一个字符写入二进制文件中模式将导致写入9位。
I''m writing a simple image file exporter as part of a school project, and
I would like to write completely platform-independent code if at all
possible. The problem I''ve run into is that apparently there''s no
guarantee that a particular implementation will have a data type that is
exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct me if I''m wrong, but I assume that if CHAR_BIT happens to be, say,
9, writing a char into a file in binary mode would result in 9 bits being
written.



只需使用fgetc和fputc写入0到255之间的整数。


如果字符为9位宽,那么在C标准中没有铸铁保证,这将产生你想要的

文件。然而在实践中将会有一个简单的方法来转换这些文件,以便它们可以被8位机器读取(可能通过转换器程序运行它们) )..

计算机存在于现实世界中并且必须相互通信。


Just use fgetc and fputc to write integers between 0 and 255.

There is no cast iron guarantee in the C standard that this will produce the
file you want, if say chars are 9 bits wide. However in practise there will
always be an easy way to convert such files so that they can be read by
8-bit machines (maybe by running them though a converter program)..
Computers exist in the real world and have to communicate with each other.


Walter Roberson写道:
Walter Roberson wrote:
我正在写一个简单的图像文件导出器作为学校项目的一部分,
我想写完全独立于平台的代码,如果有的话<可能。

PBM - 便携式BitMap(单色)
PNG - 便携式网络图形(彩色,无损压缩)
I''m writing a simple image file exporter as part of a school project,
and I would like to write completely platform-independent code if at all
possible.

PBM -- Portable BitMap (monochrome)
PNG -- Portable Network Graphics (colour, lossless compression)




我实际上正在使用PNG。



I''m actually using PNG.

我遇到的问题是显然没有
保证特定实现的数据类型恰好为8位宽; char只能保证至少为8位。
如果我错了,请纠正我,但我认为如果CHAR_BIT恰好是,请说,9,将char写入二进制文件模式将导致写入9位。
The problem I''ve run into is that apparently there''s no
guarantee that a particular implementation will have a data type that is
exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct me if I''m wrong, but I assume that if CHAR_BIT happens to be,
say, 9, writing a char into a file in binary mode would result in 9 bits
being written.



是 - 但是当您将该文件带到8位机器时会发生什么情况
未定义。


Yes -- but what happens when you take that file to an 8-bit machine
is undefined.




这正是我所担心的。

很少有文件系统(没有我知道的)存储文件
作为比特流:它们是字节或字流。以太网是比特流,但是大多数众所周知的文件传输协议都涉及8位字符。



That''s exactly what I''m worried about.
Very few filesystems (none I -know- of) store files
as bitstreams: they are byte or word streams. Ethernet is bitstream,
but most well-known file-transfer protocols involve 8-bit characters.




我在推断文件系统存储时是否正确文件为字节

流,只要有一个

并没有尝试写入值为'的'是否适合8位字节?


就标准而言,可能是一个平台,即
使用9位字符并将文件存储为比特流? :)

Kristian



Am I correct in deducing that if the file system stores files as byte
streams, it doesn''t matter how many bits wide char is as long as one
doesn''t try to write bytes with values that don''t fit in an 8-bit byte?

Could there, as far as the standard is concerned, be a platform that
uses 9-bit chars and stores files as bitstreams? :)
Kristian


这篇关于是否有标准的方法来写8位字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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