4

I am trying to store two integer value into an char array in C++. Here is the code..

char data[20];
*data = static_cast <char> (time_delay);   //time_delay is of int type
*(data + sizeof(int)) = static_cast<char> (wakeup_code);  //wakeup_code is of int type

Now on the other end of the program, I want to reverse this operation. That is, from this char array, I need to obtain the values of time_delay and wakeup_code.

How can I do that??

Thanks, Nick

P.S: I know this is a stupid way to do this, but trust me its a constraint.

0

5 Answers 5

3

I think when you write static_cast<char>, that value is converted to a 1-byte char, so if it didn't fit in a char to begin with, you'll lose data.

What I'd do is use *((int*)(data+sizeof(int))) and *((int*)(data+sizeof(int))) for both reading and writing ints to the array.

*((int*)(data+sizeof(int))) = wakeup_code;
....
wakeup_code = *((int*)(data+sizeof(int)));

Alternatively, you might also write:

reinterpret_cast<int*>(data)[0]=time_delay;
reinterpret_cast<int*>(data)[1]=wakeup_code;
Sign up to request clarification or add additional context in comments.

2 Comments

or reinterpret_cast if you use named casts
C-Style casts should not really be used anymore in C++, especially if you're not clear on the possible side effects.
3

If you are working on a PC x86 architecture then there are no alignment problems (except for speed) and you can cast a char * to an int * to do the conversions:

char data[20];
*((int *)data) = first_int;
*((int *)(data+sizeof(int))) = second_int;

and the same syntax can be used for reading from data by just swapping sides of =.

Note however that this code is not portable because there are architectures where an unaligned operation may be not just slow but actually illegal (crash). In those cases probably the nicest approach (that also gives you endianness control in case data is part of a communication protocol between different systems) is to build the integers explicitly in code one char at a time:

first_uint = ((unsigned char)data[0] |
              ((unsigned char)data[1] << 8) |
              ((unsigned char)data[2] << 16) |
              ((unsigned char)data[3] << 24));
data[4] = second_uint & 255;
data[5] = (second_uint >> 8) & 255;
data[6] = (second_uint >> 16) & 255;
data[7] = (second_uint >> 24) & 255;

Comments

1

I haven't tried it, but the following should work:

char data[20];
int value;

memcpy(&value,data,sizeof(int));

Comments

1

Try the following:

union IntsToChars {
struct {
int time_delay;
int wakeup_value;
} Integers;
char Chars[20];
};

extern char* somebuffer;

void foo()
{
    IntsToChars n2c;
    n2c.Integers.time_delay = 1;
    n2c.Integers.wakeup_value = 2;
    memcpy(somebuffer,n2c.Chars,sizeof(n2c));  //an example of using the char array containing the integer data
    //...
}

Using such union should eliminate the alignment problem, unless the data is passed to a machine with different architecture.

Comments

0
#include <sstream>
#include <string>
int main ( int argc, char **argv) {
    char ch[10];
    int i = 1234;

    std::ostringstream oss;
    oss << i;
    strcpy(ch, oss.str().c_str());

    int j = atoi(ch);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.