从文件读取二进制? [英] Reading Binary from File?
问题描述
有一个艰难的一个与此有关。
Having a tough one with this.
语言没有关系; C,PHP
Language doesn't matter; C, PHP
欲读取二进制文件,取前32比特(例如)和分配了一个int或长。该数据是二进制的,因此一个数字值,而不是字符串。然而,当我尝试这在PHP中使用FREAD()返回的数据将作为字符串(使用FOPEN(文件路径,RB))。我没有兴趣在所有字符串中的结果,只有它的自然价值(如int或long;任何数值这些位转化为)。我看了是可变比特(或字节块)的数量,我用32做为一个例子。
I want to read a binary file, taking the first 32 bits (for example) and assigning that to a int or long. The data is binary, hence a number value, not a string. Yet, when I attempt this in PHP using fread() the data is returned as a string (used fopen(path_to_file, "rb")). I'm not interested at all in a string result, only it's natural value (as INT or LONG; whatever number value those bits translate to). The number of bits (or byte blocks) I read will be variable, I used 32 here as an example.
我是用C摸索周围为好,但似乎无法掌握如何或为何值被返回的字符串(字符*),而不是数值,这是它们是什么。我的C是有点生疏,所以我可能只是不知道如何正确或不理解使用哪些使用的功能。
I was fumbling around in C as well, but cannot seem to grasp how or why the values are being returned as strings (char *) and not as number values, which is what they are. My C is a bit rusty, so I'm probably just not understanding how to use the functions correctly or not understanding which ones to use.
- 文件是1024字节。
- 要阅读的前32位,从而节省那些位作为一个数值为VAR湾
- 在b上进行逐位操作。
我在这里看到了一些帖子有类似的问题,但不是precisely。一些想出了答案似乎pretty复杂。对我来说,因为原始二进制数据是一个数值,在code做什么,我想应该是非常简单的。
I've seen a few postings here with similar questions, but not precisely. The answers some came up with seem pretty complex. To me, since raw binary data is a number value, the code to do what I want should be remarkably simple.
任何帮助,AP preciated。谢谢你。
Any help, appreciated. Thanks.
推荐答案
C ++
std::ifstream infile("thefile.bin", std::ios::binary);
uint32_t n;
infile.read(reinterpret_cast<char*>(&n), sizeof(n));
std::cout << "The value was: " << n << std::endl;
C:
FILE * fp = fopen("thefile.bin", "rb");
uint32_t n;
fread(&n, sizeof(n), 1, fp);
fprintf(stdout, "The value was: %u\n", n);
PHP:
PHP:
$fp = fopen("theinfile", "rb");
$buf = fread($handle, 4);
$res = unpack("Vn", $buf)
$n = $res['n'];
print("The value was: $n\n");
的Python:
Python:
fh = open("thefile", "rb")
data = fh.read(4)
result = struct.unpack("L", data) # result is a tuple
print "The value was", result[0]
C#:
C#:
using System;
using System.IO;
// ...
using (FileStream fs = new FileStream("thefile", FileMode.Open, FileAccess.Read))
{
using (BinaryReader r = new BinaryReader(fs))
{
System.UInt32 i = r.ReadUInt32();
Console.WriteLine("The value was: {0}", i);
}
}
后记:
%!PS-Adobe-2.0
% Open the file
/infile (thefile) (r) file def
/result 0 def
% Number of bytes to be read
/limit 4 def
/current 0 def
{
current limit ge {exit} if
infile read
not
{
/result -1 store
exit
} if
current 8 mul bitshift
result add /result exch store
/current current 1 add store
} loop
infile closefile
% Print out the result
result
/Times-Roman findfont
100 scalefont setfont
72 592 moveto
/outstr 10 string def
outstr cvs
show
showpage
数学:
n = BinaryRead["thefile.bin", "UnsignedInteger32"]
Print["The value was: ", n]
这篇关于从文件读取二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!