• C/C++: Converting 2 chars (bytes) to a short

    From Nightfox to All on Thu Jun 2 09:18:51 2011
    In C or C++, suppose there is an array of 2 chars:
    char charArray[2];

    If one wanted to convert those 2 char values to a short, I can think of 2 ways to do it:
    - Method 1 - Bit shifting and ORing:
    short shortVal = ((charArray[1] << 8) | charArray[0]);
    - Another way I've tried this:
    short shortVal = (short)(charArray[1]);
    shortVal <<= 8;
    shortVal |= charArray[0];

    - Method 2 - Using memcpy():
    short shortVal = 0;
    memcpy((void*)&shortVal, (void*)charArray, 2);

    Is either way any better than the other? And does the bit-shift & OR method look fine? I'm asking because I'm working on some C++ code for reading & writing WAV files, and when using the bit-shift/OR method to read 16-bit audio samples, the audio sounds a bit distorted, but using the memcpy() method, it sounds fine. I'm not sure why that is, unless I'm not bit-shifting or ORing correctly in the first method..

    Nightfox
  • From Digital Man@VERT to Nightfox on Thu Jun 9 16:52:31 2011
    Re: C/C++: Converting 2 chars (bytes) to a short
    By: Nightfox to All on Thu Jun 02 2011 09:18 am

    In C or C++, suppose there is an array of 2 chars:
    char charArray[2];

    If one wanted to convert those 2 char values to a short, I can think of 2 ways to do it:
    - Method 1 - Bit shifting and ORing:
    short shortVal = ((charArray[1] << 8) | charArray[0]);
    - Another way I've tried this:
    short shortVal = (short)(charArray[1]);
    shortVal <<= 8;
    shortVal |= charArray[0];

    - Method 2 - Using memcpy():
    short shortVal = 0;
    memcpy((void*)&shortVal, (void*)charArray, 2);

    Is either way any better than the other? And does the bit-shift & OR
    method look fine? I'm asking because I'm working on some C++ code for reading & writing WAV files, and when using the bit-shift/OR method to read 16-bit audio samples, the audio sounds a bit distorted, but using the memcpy() method, it sounds fine. I'm not sure why that is, unless I'm not bit-shifting or ORing correctly in the first method..

    The first way is the correct way if the charArray is big endian. The second method (memcpy) is architecture dependant and will not work on big-endian platforms. I think what you meant to do was:

    short shortVal = ((charArray[0] << 8) | charArray[1]);

    digital man

    Synchronet "Real Fact" #5:
    Synchronet version 3 for Linux and FreeBSD development began in 2001.

    ---
    ■ Synchronet ■ Vertrauen ■ Home of Synchronet ■ telnet://vert.synchro.net
  • From Nightfox to Digital Man on Sun Jun 12 00:23:12 2011
    Re: C/C++: Converting 2 chars (bytes) to a short
    By: Digital Man to Nightfox on Thu Jun 09 2011 16:52:31

    The first way is the correct way if the charArray is big endian. The second method (memcpy) is architecture dependant and will not work on big-endian platforms. I think what you meant to do was:

    short shortVal = ((charArray[0] << 8) | charArray[1]);

    Ah, thanks.. I suppose that might be where I was going wrong.

    Nightfox