14

Now there is a unsigned char bytes[4] and I've already known that the byte array is generated from an int in C++. How can I convert the array back to int?

6
  • Just << and & Commented Sep 25, 2018 at 6:54
  • 3
    Depends on how it was "generated from an int in C++". Voted to close as lacking reproducible example. Commented Sep 25, 2018 at 6:56
  • Sorry, could you explain it more clearly? @AdrianoRepetti Commented Sep 25, 2018 at 6:57
  • There's really only one way (without breaking any rules, like e.g. strict aliasing): Byte by byte copying. Either through std::memcpy (but watch out for endianness issues); Or by explicit shifting and bitwise-or to get the bytes together in the correct order. Commented Sep 25, 2018 at 6:57
  • Please note, that actually there are still platforms out there where the sizeof(int) != 4 yields true, so you'd better go with bit size instead of byte size. Commented Sep 25, 2018 at 7:00

2 Answers 2

19

You can do that using std::memcpy():

#include <iostream>
#include <cstring>

int main() {
    unsigned char bytes[4]{ 0xdd, 0xcc, 0xbb, 0xaa };

    int value;
    std::memcpy(&value, bytes, sizeof(int));

    std::cout << std::hex << value << '\n';
}
Sign up to request clarification or add additional context in comments.

5 Comments

−1 You don't know how the byte array was generated.
You are not taking into account the endianness of the byte array representation. I downvoted you but it wasn't taken into account.
+1 Ignore the comments about endianness. This does not lower the value of this solution. Often times we do not need to care about endianness because it remains the same (endianness cannot change during the execution of the program).
@Hyena it depends on the use case. If he is serializing data, and then sending it to other computers, it may cause bad surprises
Is this guaranteed to copy unaligned?
6

I've already known that the byte array is generated from an int in C++.

It is crucial to know how the array is generated from an int. If the array was generated by simply copying the bytes on the same CPU, then you can convert back by simply copying:

int value;
assert(sizeof value == sizeof bytes);
std::memcpy(&value, bytes, sizeof bytes);

However, if the array may follow another representation than what your CPU uses (for example, if you've received the array from another computer, over the network), then you must convert the representation. In order to convert the representation, you must know what representation the source data follows.

Theoretically, you would need to handle different sign representations, but in practice, 2's complement is fairly ubiquitous. A consideration that is actually relevant in practice is the byte-endianness.

3 Comments

Specifically, you should mention "endianness". The 4-byte-integer "255" can both be represented as FF000000 (small endian) or 000000FF (big endian) depending on the machine it's on. If you send integers over the network where you don't know the other machine's endianness, I wouldn't recommend simply copying raw memory: use bit shifts and masks instead.
@ZeroZ30o I do mention endianness.
Sorry, I meant mentioning it more (and the bit shift method), because I think it's the "proper" solution

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.