Click here to Skip to main content
11,408,835 members (60,561 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
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
Edited 22-Oct-12 0:42am
OriginalGriff at 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
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 294
2 Maciej Los 155
3 PIEBALDconsult 140
4 Peter Leow 140
0 Sergey Alexandrovich Kryukov 8,626
1 OriginalGriff 6,855
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034

Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2015
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