Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 16-Oct-12 1:14am
Chirma109
Edited 21-Oct-12 23:42pm
v2
Comments
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
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,680
2 CPallini 4,850
3 George Jonsson 3,454
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2014
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