如何从字节读取信息[] [英] How to read information from a byte[]
问题描述
嗨
我需要有关如何从C ++中的byte []读取信息的帮助.我知道有关数组的标头信息,例如一次要读取多少位(例如4位为integer)和顺序.
谢谢
Hi
I need help on how to read information from the byte[] in C++. I know the header information about the array like how many bits to be read at a time(for example 4 bits for integer )and the order.
Thanks
推荐答案
您的意思不大.我认为您在位和字节之间造成了混乱.我猜您想要从字节数组中提取数据.请注意,C ++中没有字节类型,可能是typedef(用于无符号字符).我也猜你在找什么,就像这样:
You''re not making too much sense. I think you make a confusion between bits and bytes. I guess what you want is to extract data from an array of bytes. Notice there is no byte type in C++, that is probably a typedef (for unsigned char). I also guess what you are looking for is something like this:
int extract_int(unsigned char[] array, size_t offset)
{
int n = 0;
memcpy(&n, array + offset, sizeof(n));
return n;
}
但是,这不能解决某些问题:
-字节序:小字节序与大字节序,以及如何将数据存储在数组中;我的样本假设它是小端字节数
-验证:您需要做一些额外的验证,例如偏移量+提取的数据类型的大小不超出数组的范围.
However, this does not address some issues:
- the endianess: little endian vs. big endian, and how your data is stored in the array; my sample assume it''s little endian
- validation: you need to do some extra validation, like that offset + sizeof the extracted data type is not beyond the boundaries of the array.
您已编辑了此问题,但未添加任何问题更多信息.如果要从C ++中读取数据块(从何处读取),请查看fstream
fstream
[ ^ ]类,或fread()
[
You have edited this question, but not added any more information. If you want to read blocks of data (from where?) in C++ then look at thefstream
[^] class, or thefread()
[^] CRT function.
这篇关于如何从字节读取信息[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!