在ANSI / ISO C ++中操作和写入字节(8位)? [英] Manipulating and writing bytes (8 bits) in ANSI/ISO C++?

查看:79
本文介绍了在ANSI / ISO C ++中操作和写入字节(8位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在写一个简单的图像文件导出器作为学校项目的一部分。要实现我选择的图像格式,我需要使用big-endian

字节,其中''byte''当然意味着''8位'',而不是'' 函数sizeof(char)的 ''。它似乎我可以使用bitset< 8>
了。在我的代码中代表一个字节---如果

你有一个更好的建议,我欢迎它---但这仍然留给我

的问题如何将这些位集以符合标准的方式写入图像文件中作为
big-endian字节。有什么想法吗?


Kristian

解决方案

2005年4月30日星期六04:49:19 +0300 ,Kristian Nybo

< kr ****** @ begone.spam.iki.fi>写道:



我正在写一个简单的图像文件导出器作为学校项目的一部分。为了实现我选择的图像格式,我需要使用big-endian
字节,其中''byte''当然意味着''8位'',而不是''sizeof(char)'' 。它好像我可以使用bitset< 8>在我的代码中表示一个字节 - 如果你有更好的建议,我欢迎它 - 但这仍然让我想到如何将这些位集写入图像文件的问题
标准兼容的大端字节。任何想法?

Kristian




在我看来,你在考虑不止一个问题。


如果我没有记错,字节顺序与文件,机器和

数据包有关,但与字节本身无关。


如果你想要8位字节从一个不是8位的字节,你可以移动

和/或填充字节。


但是如果你只关心big-endian /小端机器

兼容性,一种解决方案是将文件写入一个字节,时间为
。对于写入的文件,机器将处理机器特定的字节顺序

。如果你正在处理一个文件格式

指定字节顺序,当然你需要知道你从哪个
字节开始写入数据。


Charles


ChasW写道:

在我看来你正在考虑不止一个问题在这里。

如果我没弄错的话,字节顺序与文件,机器和数据包有关,但与字节本身有关。

如果你想要8位字节从一个不是8位的字节,你可以移动
和/或填充字节。


当然,你是对的,但我怎么把它写成8位字节?如果

char是机器上的9位代码正在运行,我该如何
只写8位?

但是如果你只关心big-endian / little endian机器的兼容性,一个解决方案就是在
时间将文件写出一个字节。对于写入的文件,机器将处理机器特定的字节顺序。




文件格式指定字节应该是big-endian,所以我需要

确保它们以这种方式编写,无论代码运行的机器是否使用big-endian或little-endian字节。也就是说,

我不希望机器处理字节序,因为我认为

会导致机器上出现''big-endian files'' big-endian字节

和little-endian文件在一个带有little-endian字节的文件上。你的意思是

别的吗?

干杯,


Kristian


< blockquote> Kristian Nybo写道:



我正在写一个简单的图像文件导出器作为学校项目的一部分。为了实现我选择的图像格式,我需要使用big-endian
字节,其中''byte''当然意味着''8位'',而不是''sizeof(char)'' 。它好像我可以使用bitset< 8>在我的代码中表示一个字节 - 如果你有更好的建议,我欢迎它 - 但这仍然让我想到如何将这些位集写入图像文件的问题
标准兼容的大端字节。任何想法?

Kristian




也许这会有所帮助吗?


man htonl

问候,

拉里


-

反垃圾邮件地址,更改每个' 'X'''''。''直接回复。


Hi,

I''m writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where ''byte'' of course means ''8 bits'', not ''sizeof(char)''. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes in a standard-compliant manner. Any ideas?

Kristian

解决方案

On Sat, 30 Apr 2005 04:49:19 +0300, Kristian Nybo
<kr******@begone.spam.iki.fi> wrote:

Hi,

I''m writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where ''byte'' of course means ''8 bits'', not ''sizeof(char)''. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes in a standard-compliant manner. Any ideas?

Kristian



Seems to me you are contemplating more than one issue here.

If I am not mistaken, endianness pertains to files, machines, and
packets, but not the bytes themselves.

If you want 8 bit bytes from a byte that is not 8 bits, you can shift
and / or pad the byte.

But if you are just concerned with big-endian / little endian machine
compatibility, one solution is to write the file out one byte at a
time. The machine will handle machine specific endianness with regard
to the file written. If you are dealing with a file format that
specifies byte order, of course you will need to be aware of which
byte you start writing the data from.

Charles


ChasW wrote:

Seems to me you are contemplating more than one issue here.

If I am not mistaken, endianness pertains to files, machines, and
packets, but not the bytes themselves.

If you want 8 bit bytes from a byte that is not 8 bits, you can shift
and / or pad the byte.
You''re right, of course, but how would I write it as an 8 bit byte? If
char is, say, 9 bits on the machine the code is running on, how do I
write only 8 bits?
But if you are just concerned with big-endian / little endian machine
compatibility, one solution is to write the file out one byte at a
time. The machine will handle machine specific endianness with regard
to the file written.



The file format specifies that the bytes should be big-endian, so I need
to make sure they''re written that way regardless of whether the machine
the code is running on uses big-endian or little-endian bytes. That is,
I don''t want the machine to handle the endianness because I assume that
would result in ''big-endian files'' on a machine with big-endian bytes
and little-endian files on one with little-endian bytes. Did you mean
something else?
Cheers,

Kristian


Kristian Nybo wrote:

Hi,

I''m writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where ''byte'' of course means ''8 bits'', not ''sizeof(char)''. It
seems that I could use bitset<8> to represent a byte in my code --- if
you have a better suggestion, I welcome it --- but that still leaves me
with the question of how to write those bitsets to an image file as
big-endian bytes in a standard-compliant manner. Any ideas?

Kristian



Perhaps this would help?

man htonl

Regards,
Larry

--
Anti-spam address, change each ''X'' to ''.'' to reply directly.


这篇关于在ANSI / ISO C ++中操作和写入字节(8位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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