Convert 2-byte array to short int
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Convert 2-byte array to short int

1. Junior Member
Join Date
Jun 2012
Location
.NET 4.0 / VS 2010
Posts
20

## 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];
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?

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);

3. Junior Member
Join Date
Jun 2012
Location
.NET 4.0 / VS 2010
Posts
20

## 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```

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);

5. Senior Member
Join Date
Jan 2009
Posts
1,689

## 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.

6. Senior Member
Join Date
Jan 2009
Posts
1,689

## 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
•