-
November 18th, 2012, 03:45 PM
#1
Convert 2-byte array to short int
I'm having trouble reading a block of bytes into a vector of short ints. My code is as follows:
Code:
FileStream.seekg(2821);
vector<short> g_Data;
int iter = 0;
g_Data.reserve(maxNumOfInts);
g_Data.resize(maxNumOfInts, 0);
while (iter<(maxNumOfInts)){
char shortBuf[2];
FileStream.read(shortBuf, 2);
g_Data[iter] = (shortBuf[1] << 8) | shortBuf[0];
iter++;
FileStream.seekg(2821+2*iter);
}
The relevant block of data starts at offset 2821 in the file. Every two bytes are a signed short integer. What's odd is that it's giving me the correct results only part of the time. At offset 1052421 and 1052422 there are two bytes 40 and 1F that are correctly read in as 8000, but at offset 1052415 and 1052416 bytes 88 and 13 are read in as -120 instead of 5000.
I don't see anything wrong with my code, though, unless I'm misunderstanding completely how to convert an array of two bytes into a single float. Is my method correct? Better still, is there some way to just convert en mass an array of bytes into a vector of signed short ints?
-
November 18th, 2012, 05:22 PM
#2
Re: Convert 2-byte array to short int
Originally Posted by fiodis
I don't see anything wrong with my code, though, unless I'm misunderstanding completely how to convert an array of two bytes into a single float. Is my method correct? Better still, is there some way to just convert en mass an array of bytes into a vector of signed short ints?
You can just write:
Code:
vector<short> g_Data(maxNumOfInts);
FileStream.seekg(2821);
FileStream.read(&g_Data[0], g_Data.size() * sizeof(g_Data[0]));
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 18th, 2012, 05:48 PM
#3
Re: Convert 2-byte array to short int
With that suggested code, I get an error:
Code:
'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'short *' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
-
November 19th, 2012, 04:35 AM
#4
Re: Convert 2-byte array to short int
Right, sorry. You will have to do a manual cast.
Code:
vector<short> g_Data(maxNumOfInts);
FileStream.seekg(2821);
FileStream.read(reinterpret_cast<char*>(&g_Data[0]), g_Data.size() * sizeof(g_Data[0]));
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 19th, 2012, 03:10 PM
#5
Re: Convert 2-byte array to short int
A problem with your code lies here
Code:
g_Data[iter] = (shortBuf[1] << 8) | shortBuf[0];
because shortBuf is an array of chars, the << 8 will move everything past the last bit. << keeps the type what it is, so you should do
Code:
g_Data[iter] = (((short)shortBuf[1]) << 8) | shortBuf[0];
Personally, I would use a union for this task however.
-
November 19th, 2012, 03:10 PM
#6
Re: Convert 2-byte array to short int
A problem with your code lies here
Code:
g_Data[iter] = (shortBuf[1] << 8) | shortBuf[0];
because shortBuf is an array of chars, the << 8 will move everything past the last bit. << keeps the type what it is, so you should do
Code:
g_Data[iter] = (((short)shortBuf[1]) << 8) | shortBuf[0];
Personally, I would use a union for this task however.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|