Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 21-Oct-12 23:42pm
v2
Comments
OriginalGriff 16-Oct-12 7:26am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

BTW: I suspect you mean "Bytes" not "Bits": please try to be accurate, as it can make a big difference to the answer!

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:

C++
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.
 
Share this answer
 
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 the fstream[^] class, or the fread()[^] CRT function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900