1

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.

3
  • If this is homework, it should be tagged as such. Commented Jan 29, 2010 at 23:09
  • 1
    Why do you want to do this? Perhaps that might help others find a better solution. Commented Jan 29, 2010 at 23:11
  • What's with people that keep saying I'm asking help for homework? It's for a personal project, I've never even taken a programming class before. @Void In my particular case I want to output the integers into a file in binary because the file itself is not developed by me, I'm just writing a program to edit said file, and the file happens to store integers in binary. Commented Jan 29, 2010 at 23:12

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

This is undefined behavior and will fail on most platforms (those which dislike unaligned access).
hence the point I made about requiring x86 platforms, still it is worth pointing out that most platforms are x86 these days anyway.
Most desktop platforms are x86-based. And it is still undefined behavior even on those.
1

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...

Comments

1

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);
}

6 Comments

That will assign the binary value of 2 once to a character array of 4 elements. That's not what I want.
There you go. Or did I misunderstood the question? Perhaps you could clarify.
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.
Works for me. Are you sure you're initializing i with {2, 2, 2} and not {2}?
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.
|
0

I think this should work:

int i,j,k;

char a[12];

*((int*)a) = i;
*(((int*)a)+1) = j;
*(((int*)a)+2) = k;

4 Comments

Like tyranid, this is undefined behavior.
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, 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.
Could you explain your answer in more detail please? Dynamic typing (as defined here: 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.

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.