Convert unsigned Char to Short

This is a discussion on Convert unsigned Char to Short within the C++ Programming forums, part of the General Programming Boards category; Hello. I have managed to read a binary file, and have put the data in a "unsigned char" array. The ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Convert unsigned Char to Short

    Hello.

    I have managed to read a binary file, and have put the data in a "unsigned char" array.

    The problem is that "unsigned char" is 1 byte large, but I need the data to be stored in a 2 byte large "short" array, because the data is a 16 bit value, being used to create picture data.

    I have made some attempts on this, but I am unsure if it works.
    This is my code so far:
    unsigned char imagebuffer[4096];
    short *Shortbuffer;

    Imagery = Ptr;// Ptr points to a place in a large "unsigned char" buffer.
    // This loop takes the first 4096 bytes of this buffer and places it
    //in it's own temporary buffer.
    for(int i=0; i<4096; i++)
    {
    imagebuffer[i] = buffer[(*Imagery+i)];
    }

    //The next thing I am trying to do is to convert this
    //temporary "unsigned buffer into a "short".
    Shortbuffer = (reinterpret_cast<short *> imagebuffer));

    My problem is that I don't know how to check if the data in the shortbuffer is now correct. As I understand it, shortbuffer is a pointer that points to"(reinterpret_cast<short *> imagebuffer));".

    But, I want the shortbuffer data to be placed in an array of it's own. (Finalshortbuffer[2048]).

    So my questions are then:
    1: will the "reinterpret_cast<short *> imagebuffer" convert the unsigned char into a short?

    2: If so how do I put the data into it's own array?

    Hope for an informativ answer.
    Kristian

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'd make a short array of sufficient size, declare a char pointer and make it point at the short array, then read the binary data to the char pointer, thus writing it into the short array. Every 2 bytes will then appear as a short (0 & 1, 2 & 3, 4 & 5 etc...).
    Code:
    short Array[WIDTH * HEIGHT];
    char* Pointer = Array;
    
    ReadFile.open(...);
    ReadFile.read(Pointer, WIDTH * HEIGHT * sizeof(short));
    ReadFile.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to recieve UDP packet
    By caroundw5h in forum Networking/Device Communication
    Replies: 15
    Last Post: 09-19-2007, 11:11 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. convert maybe maybe not..
    By pico in forum C Programming
    Replies: 7
    Last Post: 03-15-2005, 09:13 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21