Click here to Skip to main content
13,260,330 members (36,045 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.

Posted 16-Oct-12 2:14am
Updated 22-Oct-12 0:42am
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!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100