从C中的文件中读取数字

发布于 2024-12-09 13:55:12 字数 1011 浏览 0 评论 0原文

我主要是一名 Python 程序员,但我一直在使用 C,因为 Python 对于图形来说太慢了(20 fps 移动分形 FTW)。但我遇到了症结... 我在十六进制编辑器中编写了一个小文件来进行测试。当我尝试读取第一个字节 5A 时,它使用这样的程序正确地给出了 90...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned char number;
    fread(&number,1,1,data);
    printf("%d\n",number);
}

但是当我尝试将前四个字节 5A F3 5B 20 读取为整数时,我得到 542896986

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned long number;
    fread(&number,1,4,data);
    printf("%d\n",number);
}

它应该是 1525898016! ! 问题是它颠倒了字节顺序。嘎啊!当然!该程序的工作方式取决于机器。现在我们开始讨论这个问题了,即使是字节也不能在每台机器上工作!

所以我需要帮助......在Python中,我可以使用struct.pack和struct.unpack使用任何格式(长,短,单,双,有符号,无符号,大端,小端)将数据打包成字节并解压它。我需要在 C 中编写这样的东西...我可以自己编写,但我不知道如何编写。

I am primarily a Python programmer, but I've been working with C because Python is too slow for graphics (20 fps moving fractals FTW). I'm hitting a sticking point though...
I wrote a little file in a hex editor to test with. When I try reading the first byte, 5A, it correctly gives me 90 with a program like this...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned char number;
    fread(&number,1,1,data);
    printf("%d\n",number);
}

But when I try reading the first four bytes, 5A F3 5B 20, into an integer I get 542896986

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
    data=fopen("C:\\vb\\svotest1.vipc","r+b");
    unsigned long number;
    fread(&number,1,4,data);
    printf("%d\n",number);
}

It should be 1525898016!!!
The problem is it has reversed the byte order. GAH! Of course! The way this program works will depend on the machine. And now that we are on the subject, even the byte won't work on every machine!

So I need help... In Python I can use struct.pack and struct.unpack to pack data into bytes using any format (long, short, single, double, signed, unsigned, big endian, little endian) and unpack it. I need something like this in C... I could write it myself but I don't know how.

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

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

发布评论

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

评论(3

兮子 2024-12-16 13:55:12

可移植地处理此问题的最简单方法可能是在将数据写入文件之前对数据使用 htonl (因此文件将采用网络/大端顺序)和 ntohl 当您阅读时(将网络订单数据转换为本地约定,无论是什么)。

如果您必须执行的操作不仅仅是一种类型的几个值,您可能需要为此目的查看一个更完整的库,例如 Sun XDR 或 Google 协议缓冲区

The easiest way to handle this portably is probably to use htonl on the data before you write it to the file (so the file will be in network/big endian order) and ntohl when you read (converts the network order data to the local convention, whatever that is).

If you have to do much more than a few values of one type, you may want to look into a more complete library for the purpose, such as Sun XDR or Google Protocol Buffers.

夜血缘 2024-12-16 13:55:12

I think you can figure out the (Endianness) http://en.wikipedia.org/wiki/Endianness and then read the byte order.

沉溺在你眼里的海 2024-12-16 13:55:12

像这样?

unsigned long bin = 0;
unsigned char temp = 0;

for(unsigned char i = 0; i < 4; i++) {
    fread(&temp,1,1,data);
    bin = (bin << 8) | temp;
}

这导致第一部分是最重要的。我想这就是你想要的。

Like this?

unsigned long bin = 0;
unsigned char temp = 0;

for(unsigned char i = 0; i < 4; i++) {
    fread(&temp,1,1,data);
    bin = (bin << 8) | temp;
}

This results in the first part being the most significant. I think that's what you want.

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