Char *操作 [英] Char* operation
问题描述
对不起,
无法想出一个更好的头衔,但这就是我要做的事情:
我正在制作一个小型控制台应用程序来读取.pak文件(它们来自游戏)。我正在逐步构建它,所以我不会犯错误。
现在我正在尝试获取存储在这样的.pak文件中的文件数量。这通常是通过从.pak文件读取前2个字节,将它们反转然后将它们解释为标题来完成的。
我能够通过使用fstream对象读取前2个字节(ifstream)并将2个字节读入char *缓冲区。
现在我有问题,如何反转2个字节,然后将其解释为整数/短。 ?
我尝试在char *中切换地方的字节(作为数组处理),但没有给我我想要的结果。
示例:
24 00应读为00 24,结果为36整数。
Sorry,
couldn''t come up with a better title, but here''s what I''m trying to do:
I''m making a small console application that reads .pak files (they''re from a game). I''m building it in steps, so I don''t make errors.
Right now i''m trying to obtain the amount of files stored inside such a .pak file. This is normally done by reading the first 2 bytes from the .pak files, reversing them and then interpreting them as a header.
I''m able to read the first 2 bytes by using an fstream object (ifstream) and read 2 bytes into a char* buffer.
Now I have as question, how can I reverse the 2 bytes, and then interpret it as an integer/short. ?
I tried switching the bytes of place inside the char* (treated as array) but didn''t give me the result I wanted.
example :
24 00 should be read as 00 24, and resulting in 36 as integer.
推荐答案
这两个字节在你的文件中存储了big endian,你希望它们在
endian命令中。在short int中读取这两个字节并将ntohs()宏
应用于此:
The two bytes are stored big endian in your file and you want them to be in little
endian order. Read those two bytes in a short int and apply the ntohs() macro
to it like this:
好的,我设法得到了更多。
这里是我正在使用的代码,它是非常粗糙的只是得到此刻只有2个字节:
Ok, I managed to get a bit further.
Here''s the code I''m currently using, it''s very rough just to get the 2 bytes at the moment, nothing else:
没关系找到它:)
必须链接到ws2_32.lib
但现在我得到的输出:金额Map.pak中的文件:9216
但它应该是:Map.pak中的文件数量:36。
是转换进行错了呢?
never mind found it :)
had to link to ws2_32.lib
but now I get as output : Amount of files in Map.pak : 9216
but it should be : Amount of files in Map.pak : 36.
is the conversion going wrong then?
这篇关于Char *操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!