Thread: Store 16 bit words in char array?

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should check what the other end is expecting (i.e. maybe the other end is expecting little-endian numbers, in which case everybody's happy, although that's not all that likely). If you need to convert to big-endian, then there is htons (since "network order" is another name for "big-endian").

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    Something like this may be better:
    Code:
    void packetBuilder ( int count, string package ) {
        short int *pack = new short int[count+4]; //don't know why you have count+5 there at all
        stringstream dataStream( package );
    
        // Save 0-3 for other purposes
    
        for ( int i = 4; i < count+4; i++) {
            dataStream >> hex >> pack[i];
        }
        sendData (static_cast<char *>(pack));
        free(pack);
    }
    This is undefined behavior (new and free).
    But what I don't understand is that since you have the data in a string, why not simply do
    Code:
    sendData(package.c_str());
    ?

    Are you implying that your string consists of a number of shorts added together? That is, if we do
    Code:
    const short* p = reinterpret_cast<const short*>(package.c_str());
    Then p[0] will be the first block of data and p[1] the next, etc?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    That does sound reasonable. There are spaces between the hex values, but I can easily read them all into a string without spaces and send them without risk of flipping anything.
    I just talked to the guy working the other end and he is just dealing with the 1's and 0's on his end, knowing this information, sending the data in a string would be easier. Earlier this week he gave me the impression that he was expecting an array he could read to fly out the other side.
    Gotta love introverted programmers who don't communicate and waste your time :-(

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Mmm. Does he want the values, or the textual representation? That is, if you have 0x1234, you can send two bytes 0x12 and 0x34 (in some order), or you can send the five bytes 0x31 (ASCII '1'), 0x32, 0x33, 0x34, 0x00 (for the end of the string).

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    He wants the actual values, padded with 0's. So if the number is 0x0F it needs to be sent as 000F. I think I have that already figured out though, but thank you for all the help! Even if I am not using the original scheme, I learned some new tricks and a bit about Endians so it was a worth-while detour.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you have to make sure you pad the values correctly when you add them to the string. I don't know how you are receiving these values, though.
    But if you have an array of shorts, you might actually do this instead:
    Code:
    std::vector<char> data;
    // Let input be an array of shorts whose length is input_length
    for (int i = 0; i < input_length; i++)
    {
    	data.push_back(input[i] & 0x00FF);
    	data.push_back((input[i] & 0xFF00) >> 8);
    }
    SendData(&data[0]);
    This will avoid endianess issues.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to store a char array into another char array?
    By DaNxTh3xMaNx in forum C Programming
    Replies: 4
    Last Post: 11-12-2010, 10:28 AM
  2. Flip words in a char array.
    By mherald81 in forum C Programming
    Replies: 21
    Last Post: 10-17-2010, 09:11 AM
  3. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  4. char array of words
    By sujeet1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 07:27 AM
  5. How to randomize words in a char array?
    By fero45 in forum C++ Programming
    Replies: 2
    Last Post: 05-31-2002, 07:01 PM