ARITHMETIC WORKSHEETS

Checking byte order before interpreting a two-byte integer

Read the same two bytes in both byte orders and see why the integer value depends on which byte carries the larger place value.

Two bytes, two place values

A single byte holds a value from 0 to 255, written as two hexadecimal digits from 00 to FF. Two bytes together can form a 16-bit integer, but only once you decide which byte is worth 256 times as much as the other. If the byte 01 comes first and carries the higher weight, the value is 1 times 256 plus 2, which is 258. If the same pair is read with the first byte as the lower weight, the value is 1 plus 2 times 256, which is 513. The bytes on the wire or in the file have not changed. Only the convention for assembling them has changed, and the integer is different in each case.

Most significant byte first

In the order often called big-endian, the byte written first is the most significant. Network protocols in the Internet family conventionally send multi-byte fields this way, which is why it is also called network byte order. Reading the bytes 01 02 in that order is the same as reading the hexadecimal integer 0102. The Integer Base Converter, with from base 16 and to base 10, returns 258 for the string 0102. The leading zero is harmless, because it adds nothing to the place values. Human-readable hexadecimal dumps also read left to right with the high digit first, so this order matches how most people write a number down.

Least significant byte first

In the opposite convention, called little-endian, the byte stored first is the least significant. Many common processor families store multi-byte integers in memory this way. To interpret the bytes 01 02 under this rule, reverse them into 02 01 and read that as an ordinary hexadecimal integer. The converter with the string 0201 from base 16 to base 10 returns 513. That reconstruction step is the point of the guide: the converter only reads digit strings left to right with the first digit most significant, so you must rearrange the bytes yourself before entering them. Mixing up which order a source uses is a classic cause of file-format and protocol bugs. A useful habit is to write the reversed digit string next to the original in your notes, so both candidate values are visible before you decide.

A second pair, 12 and 34

Take the bytes 12 34. As most-significant-first, they form 0x1234, which equals 1 times 4,096 plus 2 times 256 plus 3 times 16 plus 4, or 4,660. Reversed into 3412, the same bytes form 13,330. The difference between the two readings is 8,670, and it is not a small rounding effect, because swapping bytes changes every place value. Entering 1234 and then 3412 into the converter, both from base 16 to base 10, confirms the numbers. Notice that neither result is wrong in isolation. Each is correct for its convention, and only knowing the source format tells you which one applies.

Edge cases with zero bytes

Swapping bytes can change a value dramatically when one byte is zero. The pair FF 00 read most-significant-first is hexadecimal FF00, which is 65,280. The same pair read least-significant-first becomes 00FF, which is 255. Those two values differ by a factor of 256, since the nonzero byte moved from the high place to the low place. This case explains why short test values like 255 can pass in a buggy program that has the wrong byte order: a value that fits in one byte hides the problem, while values above 255 expose it. Use test data that fills both bytes with different nonzero values, as 12 34 does. Keep the digit pairs in hexadecimal while you work, because 0xFF is easier to read as a full byte than the decimal 255.

Why the converter cannot guess the order

The Integer Base Converter accepts a whole integer written as a digit string and a pair of bases. It does not accept a list of bytes separated by spaces and has no setting for byte order. It parses the digits as one number and formats the result in the target base. That is the right behavior for a base conversion, and it means the byte-order decision belongs to you, made before the converter is used. Establish the source format first, such as a protocol document that states big-endian fields, then assemble the digit string in the right order, then convert. Skipping the first step gives a clean-looking but wrong integer.

Where byte order is documented

Standards for file formats and protocols state the order for every multi-byte field. Internet protocol specifications describe network byte order as big-endian, and many binary file formats declare either order in a header or in the format specification. Some formats, such as TIFF, include a marker at the start of the file to say which order the file uses. The terms big-endian and little-endian were popularized in computing by a 1980 paper by Danny Cohen, borrowing the names from the opposing factions in Jonathan Swift's novel. When documentation is unclear, a field with a known value, such as a length or a version number, can reveal the order by which reading gives a sensible result.

Beyond two bytes

The same idea extends to four and eight bytes. A four-byte integer has place values of 1, 256, 65,536 and 16,777,216, and the order convention decides which byte takes which weight. Reversing all four bytes yields a different integer in nearly every case. A palindromic byte pattern, such as 01 02 02 01, is unchanged by reversal and therefore unable to reveal an order mistake. When designing test inputs for any conversion routine, pick bytes that are all different. With two bytes, 01 02 is the smallest convenient choice, and with four, 01 02 03 04 gives 16,909,060 in one order and 67,305,985 in the other.

Signed values and other limits

The worksheet covers unsigned interpretation of two bytes. Signed integers use a different encoding for negative values, and the sign bit sits in the most significant byte, so byte order interacts with sign handling in a way this guide does not model. The guide also does not treat floating point numbers, text encodings with byte order marks, or bit order within a byte. Each of those has its own convention. For the two cases shown, 258 and 513, the conversions are exact and can be repeated in the converter, and the principle carries over: decide the order first, then assemble the digits, then convert.

Reproduce the checks

Most-significant byte first

Open the Integer Base Converter and enter:

Value
0102
Source base
16
Target base
10

Expected output:

Value

258

The first byte occupies the higher place.

Reconstructed opposite order

Open the Integer Base Converter and enter:

Value
0201
Source base
16
Target base
10

Expected output:

Value

513

Reversing the byte significance changes the integer.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks