# Convert 2-byte array to short int

• November 18th, 2012, 02:45 PM
fiodis
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, 04:22 PM
D_Drmmr
Re: Convert 2-byte array to short int
Quote:

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]));```
• November 18th, 2012, 04:48 PM
fiodis
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, 03:35 AM
D_Drmmr
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]));```
• November 19th, 2012, 02:10 PM
ninja9578
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, 02:10 PM
ninja9578
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.