tags:

views:

95

answers:

4

Hi, I have three integers (4 bytes of memory for each integer) and I want to assign each of their binary values to a character array with 12 elements. So, if each integer had a value of let's say 2, then I want the character array to have these values:

2 0 0 0 2 0 0 0 2 0 0 0

I have tried:

memcpy(cTemp, &integer1 + &integer2 + &integer3, 12);

but I receive an "invalid operands" compiler error.

I have also found the function strcat referenced here: http://www.cplusplus.com/reference/clibrary/cstring/

However it is mentioned as: "The terminating null character in destination is overwritten by the first character of source" which I obviously don't want since most of the times integers will have a null character at the end unless the value is really large. Does anybody know of a better working method? Any help is appreciated.

+1  A: 

If you want to see how a variable is represented as a sequence of bytes, you can do the following.

int i[3] = {2, 2, 2};
char cTemp[sizeof i];
memcpy(cTemp, &i, sizeof i);

Note however that the representation will be different on different platforms. What are you trying to solve?

Edit:

I'm just writing a program to edit [a file], and the file happens to store integers in binary.

Why didn't you say so in the first place? If you know the program will only run on platforms where int has the correct memory-layout, you can simply store the integer.

fout.write((char const *)&i, sizeof i);

However, if you want to be portable, you need to properly serialize it.

void store_uint32_le(char * dest, unsigned long value)
{
    for (int i = 0; i < 4; ++i)
    {
        *dest++ = value & 0xff;
        value >>= 8;
    }
    assert(value == 0);
}

int main()
{
    char serialized[12];
    store_uint32_le(serialized, 2);
    store_uint32_le(serialized + 4, 2);
    store_uint32_le(serialized + 8, 2);

    std::ofstream fout("myfile.bin", std::ios::binary);
    fout.write(serialized, sizeof serialized);
}
avakar
That will assign the binary value of 2 once to a character array of 4 elements. That's not what I want.
kaykun
There you go. Or did I misunderstood the question? Perhaps you could clarify.
avakar
I've tried this method, and it only appears to copy the memory of the first element of i. I've even tried changing the third parameter of memcpy to 12.
kaykun
Works for me. Are you sure you're initializing `i` with `{2, 2, 2}` and not `{2}`?
avakar
Yes. To tell if it worked or not I outputted the contents of cTemp into a file using ofstream, and I only got 2 0 0 0.
kaykun
You mean `cTemp` only has 4 elements? You'll probably need to show some code. The snippet in my answer works fine and should work under any compliant compiler.
avakar
+1  A: 

It is probably simpler (if you are on a x86 at least :P) to just cast the pointer and assign directly. i.e.

int* p = (int*) cTemp; 
p[0] = a;
p[1] = b;
p[2] = c;
tyranid
This is undefined behavior and will fail on most platforms (those which dislike unaligned access).
avakar
hence the point I made about requiring x86 platforms, still it is worth pointing out that _most_ platforms are x86 these days anyway.
tyranid
Most *desktop* platforms are x86-based. And it is still undefined behavior even on those.
avakar
+1  A: 

You can also do a union hack:

union translate {
    char c[sizeof(int) * 3];
    int i[3];
};

translate t;
t.i[0] = 2;
t.i[1] = 2;
t.i[2] = 2;

// access t.c[x] to get the chars

... and read the chars...

Kornel Kisielewicz
A: 

I think this should work:

int i,j,k;

char a[12];

*((int*)a) = i;
*(((int*)a)+1) = j;
*(((int*)a)+2) = k;
abc
Like tyranid, this is undefined behavior.
avakar
I presume you mean that some platform might word-align the char array elements. Because arrays are defined as contiguous, that would mean sizeof(char)==word_length (e.g. 4). Could you give me an example of an architecture for which this is true?
abc
abc, the standard does not define the behavior of code that accesses objects of dynamic type `int` through l-values of static type `char`. That's why the behavior is undefined. And implementation are not allowed to add padding between elements of an array.
avakar
Could you explain your answer in more detail please? Dynamic typing (as defined here: http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing) does not exist in c++. It would also help if you could point me to the relevant passage in the standard. Thanks.
abc