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

Thread: Convert 2-byte array to short int

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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    Re: Convert 2-byte array to short int

    Quote Originally Posted by fiodis View Post
    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

  3. #3
    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. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    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

  5. #5
    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. #6
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center