2

I have the following char array:

char* a = new char[6]{0};

Which in binary is:

00000000 00000000 00000000 00000000 00000000 00000000

I also have an integer:

int i = 123984343;

Which in binary is:

00000111 01100011 11011001 11010111

I would like to insert this 4-byte-integer i into the char array a from position [1] to position [4] so that the original array a becomes:

00000000 00000111 01100011 11011001 11010111 00000000

What is the quickest and easiest method to do that?

3
  • 3
    I smell an XY problem. What are you trying to accomplish? This probably isn't the best way to do it. Commented Dec 28, 2015 at 19:49
  • 2
    This is wrong on many levels, since the representation of signed integers is implementation defined, the same is how many bits a char and an int have. A char could be as big as an int. Use unsigned fixed with types, instead. Commented Dec 28, 2015 at 19:49
  • 1
    How do you know that is the representation of the int in binary. There are other platforms where it is 1101011 11011001 0110011 00000111. There are a total of at least six different possible binary representations if the integer is negative. Commented Dec 28, 2015 at 19:53

3 Answers 3

3

You can solve the problem as asked with

    memcpy( &a[1], &i, sizeof(i) );

but I bet dollars to doughnuts that this is not the best way of solving your problem.

    for (size_t ix = 0; ix < 4; ix++)
    {
        a[1+ix] = (static_cast<unsigned int>(i) >> (8*ix)) & 0xff;
    }

Is a safe way of serializing an int which fits into four bytes into a character array. Neither this end, nor the other end, have to make non-portable assumptions.

I'm not convinced that even this is the best way of solving your actual problem (but it hard to tell without more information).

Sign up to request clarification or add additional context in comments.

Comments

3

Use the copy algorithm and a cast to char to access the underlying byte sequence:

#include <algorithm>
#include <cstdint>

std::uint32_t n = 123984343;
char * a = new char[6]{};

{
    const char * p = reinterpret_cast<const char *>(&n);
    std::copy(p, p + sizeof n, a + 1);
}

In this case I am assuming that you are guaranteeing me that the bytes of the integer are in fact what you claim. This is platform-dependent, and integers may in general be laid out differently. Perhaps an algebraic operation would be more appropriate. You still need to consider the number of bits in a char; the following code works if uint8_t is supported:

std::uint8_t * p = reinterpret_cast<std::uint8_t *>(a);
p[1] = n / 0x1000000;
p[2] = n / 0x0010000;
p[3] = n / 0x0000100;
p[4] = n / 0x0000001;

4 Comments

Argh, yes, char is an exception to the strict-aliasing rule... forgot that.
Thanks, I'll test it tomorrow!
Why does the last example not break the strict-aliasing rule?
@cad: Hm, yeah. I'm assuming that if uint8_t exists, then it is an alias for one of the char types. Perhaps that warrants a static assertion. The point is that this code can only work if CHAR_BIT is 8, so I wanted to encode that. Alternatively you can use unsigned char and assert that that has eight bits.
0

If int is of 4 bytes, then copy the int to address of 2nd position in the char array.

int i = 123984343;  
char* a = new char[6]{0};   
memcpy(a+1, &i, sizeof(i));

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.