文件格式中的整数类型

发布于 2024-09-03 20:49:11 字数 258 浏览 5 评论 0原文

我目前正在尝试学习一些更深入的文件格式知识。

我有一个 3D 文件格式的规范(在本例中为 U3D),我想尝试实现它。没什么大不了的,只是为了学习效果。

我的问题很早就开始于需要定义的类型。我必须定义不同的整数(8 位、16 位、32 位无符号和有符号),然后需要将它们转换为十六进制,然后再将其写入文件。

由于我不能只创建 I16 ie,因此如何定义这些类型? 对我来说另一个问题是如何将 I16 转换为 8 位十六进制数 (即 0001 0001)。

I am currently trying to learn some more in depth stuff of file formats.

I have a spec for a 3D file format (U3D in this case) and I want to try to implement that. Nothing serious, just for the learning effect.

My problem starts very early with the types, that need to be defined. I have to define different integers (8Bit, 16bit, 32bit unsigned and signed) and these then need to be converted to hex before writing that to a file.

How do I define these types, since I can not just create an I16 i.e.?
Another problem for me is how to convert that I16 to a hex number with 8 digits
(i.e. 0001 0001).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我入戏太深 2024-09-10 20:49:11

十六进制只是数字的表示。您是否将数字解释为二进制、十进制、十六进制、八进制等取决于您。在 C++ 中,您支持十进制、十六进制和八进制表示形式,但它们都以相同的方式存储。

示例:

int x = 0x1;
int y = 1;
assert(x == y);

文件格式可能希望您以正常的二进制格式存储文件。我认为文件格式不需要十六进制数字作为可读的文本字符串。如果确实如此,那么您可以使用 std::hex 为您进行转换。 (例如:file << hex << number;

如果文件格式涉及向文件写入超过 1 个字节的类型,则请注意 架构的字节序。这意味着您首先还是最后存储多字节类型的最高有效字节。

在文件格式规范中,向您展示二进制文件应如何查找文件的给定部分是很常见的。不要将其与实际将二进制数字存储为字符串相混淆。同样,他们有时会通过以十六进制指定它的外观来提供快捷方式。同样,大多数时候它们实际上并不意味着文本字符串。

C++ 中的最小可寻址单元是 char,它是 1 个字节。如果要设置该字节内的位,则需要使用按位运算符,例如 &|。关于按位运算符的教程有很多,这里不再赘述。

Hex is just a representation of a number. Whether you interpret the number as binary, decimal, hex, octal etc is up to you. In C++ you have support for decimal, hex, and octal representations, but they are all stored in the same way.

Example:

int x = 0x1;
int y = 1;
assert(x == y);

Likely the file format wants you to store the files in normal binary format. I don't think the file format wants the hex numbers as a readable text string. If it does though then you could use std::hex to do the conversion for you. (Example: file << hex << number;)

If the file format talks about writing more than a 1 byte type to file then be careful of the Endianness of your architecture. Which means do you store the most significant byte of the multi byte type first or last.

It is very common in file format specifications to show you how the binary should look for a given part of the file. Don't confuse this though with actually storing binary digits as strings. Likewise they will sometimes give a shortcut for this by specifying in hex how it should look. Again most of the time they don't actually mean text strings.

The smallest addressable unit in C++ is a char which is 1 byte. If you want to set bits within that byte you need to use bitwise operators like & and |. There are many tutorials on bitwise operators so I won't go into detail here.

友谊不毕业 2024-09-10 20:49:11

如果您包含 您将获得如下类型:

uint8_t
int16_t
uint32_t

If you include <stdint.h> you will get types such as:

uint8_t
int16_t
uint32_t
烙印 2024-09-10 20:49:11

首先,让我了解一下。
整数以十六进制格式作为文本存储在文件中,没有前缀 0x?

然后,使用以下语法:

fprintf(fp, "%08x", number);

将把 0abc1234 写入文件中。

First, let me understand.
The integers are stored AS TEXT in a file, in hexadecimal format, without prefix 0x?

Then, use this syntax:

fprintf(fp, "%08x", number);

Will write 0abc1234 into a file.

梦年海沫深 2024-09-10 20:49:11

至于“定义不同的整数(8位、16位、32位无符号和有符号)”,除非您为它们进行自己的数学运算,否则您应该坚持使用系统提供的类型。请参阅 stdint.h 了解可用的类型定义,例如 int32_t。

As for "define different integers (8Bit, 16bit, 32bit unsigned and signed)", unless you roll your own, and concomitant math operations for them, you should stick to the types supplied by your system. See stdint.h for the typedefs available, such as int32_t.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文