Int to byte array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting an integer to a byte array is essential for binary protocols, network communication, file I/O, and serialization. The key consideration is byte order (endianness) — big-endian stores the most significant byte first, while little-endian stores the least significant byte first. Most network protocols use big-endian (network byte order), while x86 CPUs use little-endian. Every language provides built-in methods for this conversion with explicit endianness control.
Python
struct format characters: > big-endian, < little-endian, I unsigned int (4 bytes), Q unsigned long long (8 bytes), H unsigned short (2 bytes).
Java
C#
JavaScript
C/C++
Go
Common Pitfalls
- Ignoring endianness: Systems use different byte orders. Sending little-endian bytes to a big-endian receiver corrupts the value. Always agree on endianness in protocols — network byte order (big-endian) is the standard convention.
- Wrong byte array size: A 32-bit
intneeds 4 bytes, a 64-bitlongneeds 8 bytes. Using the wrong size truncates or pads the value. Match the byte array size to the integer type's bit width. - Signed vs unsigned interpretation: The same bytes
[0xFF, 0xFF, 0xFF, 0xFF]represent -1 as a signed int or 4294967295 as an unsigned int. Use the matching signed/unsigned conversion function for your data type. - Using
BitConverterin C# without checking endianness:BitConverter.GetBytes()uses the system's native endianness, which is little-endian on x86. For portable code, useBinaryPrimitiveswith explicit big/little endian methods. - Integer overflow when converting back: Reconstructing a 32-bit int from bytes using bit shifts in languages without unsigned types (like Java) can produce unexpected negative values. Use
& 0xFFmasks on each byte to prevent sign extension.
Summary
- Use
int.to_bytes()/struct.pack()in Python,ByteBufferin Java,BitConverter/BinaryPrimitivesin C# - Always specify endianness explicitly — big-endian for network protocols, little-endian for x86 native
htonl()/ntohl()in C convert between host and network byte order- JavaScript uses
DataViewfor endianness control, Node.js usesBuffer - Match byte array size to integer bit width (4 bytes for 32-bit, 8 bytes for 64-bit)
- Use
& 0xFFmasks when reconstructing integers from bytes to prevent sign extension
Related reading
- Internal Implementation of STLMAP in C
- Interpreting Dijkstra's Algorithm
- Intersection and union of ArrayLists in Java
- Intersection of multiple lists with IEnumerable.Intersect
- Interview challenge Find the different elements in two arrays
- Interview question - Search in sorted array X for index i such that Xi i
- Interview Question Data structure for a large social network
- Interview Question Find Median From Mega Number Of Integers

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.