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
?

- 473
- 1
- 6
- 18
-
Just `<<` and `&` – Adriano Repetti Sep 25 '18 at 06:54
-
3Depends on how it was "generated from an int in C++". Voted to close as lacking reproducible example. – Cheers and hth. - Alf Sep 25 '18 at 06:56
-
Sorry, could you explain it more clearly? @AdrianoRepetti – maxwellhertz Sep 25 '18 at 06:57
-
There's really only one way (without breaking any rules, like e.g. [*strict aliasing*](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule)): Byte by byte copying. Either through [`std::memcpy`](https://en.cppreference.com/w/cpp/string/byte/memcpy) (but watch out for [*endianness*](https://en.wikipedia.org/wiki/Endianness) issues); Or by explicit shifting and bitwise-or to get the bytes together in the correct order. – Some programmer dude Sep 25 '18 at 06: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. – Ferenc Deak Sep 25 '18 at 07:00
-
You can take a look at https://github.com/fritzone/nap-script/blob/master/vm/byte_order.c where this problem is solved in a more or less platform independent note. Please bear in mind that the example code uses little endian encoding, so if your platform is different than that this might not be a feasible solution for you. (apologies for giving link to own project, albeit it's open source :) ) – Ferenc Deak Sep 25 '18 at 07:03
2 Answers
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';
}

- 12,971
- 3
- 21
- 43
-
7
-
2You are not taking into account the endianness of the byte array representation. I downvoted you but it wasn't taken into account. – user1741137 Apr 13 '20 at 14:13
-
3+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 Sep 15 '20 at 12:40
-
1@Hyena it depends on the use case. If he is serializing data, and then sending it to other computers, it may cause bad surprises – ZeroZ30o Aug 12 '21 at 22:31
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.

- 232,697
- 12
- 197
- 326
-
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 Aug 12 '21 at 22:30
-
-
Sorry, I meant mentioning it more (and the bit shift method), because I think it's the "proper" solution – ZeroZ30o Aug 13 '21 at 12:12