Float to Hex Converter
Float to hex converter: type a decimal number and get its IEEE 754 bytes in hex, single or double precision. 0.1 is 3DCCCCCD as a single and 3FB999999999999A as a double. Choose the byte order and see the rounding error between what you typed and what is stored.
| Sign | 0 (positive) |
|---|---|
| Exponent field | 01111011 = 123, unbiased -4 |
| Mantissa field | 10011001100110011001101 |
| Class | normal |
| Exact stored value | 0.100000001490116119384765625 |
| Value you typed, as a double | 0.1000000000000000055511151231257827021181583404541015625 |
| Rounding error | 1.490e-9 |
Float to hex: store the sign, the exponent and the mantissa
To convert a floating point number to hex, write it in binary as 1.xxx times a power of two, then pack three fields: the sign bit, the exponent plus a bias, and the digits after the point. For a 32-bit float that is 1, 8 and 23 bits with a bias of 127, and for a 64-bit double it is 1, 11 and 52 bits with a bias of 1023. The 32 bits, read in groups of four, are the hex.
The diagram is 0.1 as a single. Its exponent field is 123, which is -4 after the bias, because 0.1 is 1.6 times 2 to the power of −4. The mantissa holds the 0.6 as a binary fraction. The converter above does these steps and shows the three fields in the table below the result.
Doing it by hand with a number that fits exactly: 0.15625
Choose a number that is a sum of powers of two, and the arithmetic comes out clean. 0.15625 is 5/32, which in binary is 0.00101. Shift the point until one 1 is in front of it: 0.00101 is 1.01 times 2 to the power of −3. The exponent field is −3 + 127 = 124, which is 01111100 in 8 bits. The mantissa is the part after the point, 01, followed by zeros to fill 23 bits. The sign is 0.
| Step | Result |
|---|---|
| Binary | 0.00101 |
| Normalized | 1.01 × 2^−3 |
| Exponent field | −3 + 127 = 124 = 01111100 |
| Mantissa field (23 bits) | 01000000000000000000000 |
| All 32 bits | 0 01111100 01000000000000000000000 |
| Regrouped in fours | 0011 1110 0010 0000 0000 0000 0000 0000 |
| Hex | 3E200000 |
Regrouping is the step that trips people up: the field boundaries (1, 8, 23) do not line up with the hex digits, so you write all 32 bits in a row and only then cut them into fours. The page on hex to binary shows the lookup for each group.
Why 0.1 ends in CD: repeating fractions are rounded
The number 0.1 in binary is 0.000110011001100110011, repeating the group 0011 forever. A mantissa with 23 bits can hold only part of it, so the encoder keeps the closest 23 bits. The bits after the cut start with 1100, which is more than half, so the last kept bit is rounded up. Before rounding the mantissa would end in …1100 (hex C); after rounding it ends in …1101 (hex D). That is how the pattern 3DCCCCCC turns into 3DCCCCCD.
The stored number is then 0.100000001490116119384765625, a little above one tenth. The 64-bit pattern, 3FB999999999999A, does the same with 52 bits and ends in A instead of 9 for the same reason. The shortcut for recognizing such values is the repeating pattern in the hex: a run of C, 9, 3 or A digits with a rounded last digit is a decimal fraction with no exact binary form.
Rounding error: stored value against the number you typed
The difference between what you typed and what is stored is the rounding error. Singles carry it in the eighth significant digit, doubles in the seventeenth. The table is computed with the same code as the converter, and it shows that the error depends on the number: 0.5 is exact, 0.1 is not, and a larger number such as 100.1 has a larger absolute error even though the relative error is similar.
| Typed | Single (hex) | Single stored exactly | Single error | Double (hex) |
|---|---|---|---|---|
| 0.1 | 3DCCCCCD | 0.100000001490116119384765625 | 1.49e-9 | 3FB999999999999A |
| 0.2 | 3E4CCCCD | 0.20000000298023223876953125 | 2.98e-9 | 3FC999999999999A |
| 0.3 | 3E99999A | 0.300000011920928955078125 | 1.19e-8 | 3FD3333333333333 |
| 0.5 | 3F000000 | 0.5 | none | 3FE0000000000000 |
| 1.1 | 3F8CCCCD | 1.10000002384185791015625 | 2.38e-8 | 3FF199999999999A |
| 100.1 | 42C83333 | 100.09999847412109375 | -1.53e-6 | 4059066666666666 |
| 1e-7 | 33D6BF95 | 0.00000010000000116860974230803549289703369140625 | 1.17e-15 | 3E7AD7F29ABCAF48 |
Because the error is built into the format, comparing floats for equality after arithmetic is unreliable, and the safe habit is to compare with a tolerance or to compare the hex patterns when you need bit-for-bit equality. The exact decimal of a typed number as a double is also shown by the converter, for example 0.1000000000000000055511151231257827021181583404541015625 for 0.1.
Integers as floats: where counting stops
Whole numbers are exact in a float only while they fit in the 24 significant bits of a single (up to 16,777,216) or the 53 of a double (up to 9,007,199,254,740,992). Beyond that, the spacing between neighbours is 2 or more, and some integers are skipped. 16,777,216 is 4B800000, and 16,777,217 maps to the same pattern, 4B800000, because it sits exactly halfway between two singles and rounds to the one with an even mantissa.
This matters when a counter, an identifier or a timestamp is stored as a float. If the value must stay exact, store it as an integer and convert it with decimal to hex instead. Floats are for quantities that are measured or computed, where the last digit is not meant to be exact.
Negative numbers and negative zero
A float stores sign and magnitude. Flipping the top bit negates the number and nothing else changes, so 2.0 is 40000000 and −2.0 is C0000000. That is different from integers, where negatives use two's complement and every bit changes. The top bit is also why the hex of a negative float starts with 8, 9, A, B, C, D, E or F in the first digit.
Zero has a negative form, 80000000, produced by arithmetic such as −1 × 0 and entered here as -0. It compares equal to 0 but keeps its sign, which shows up in a few places such as 1 ÷ −0, and in the sign of a rounded tiny negative value.
Too large and too small: overflow and underflow
A single holds finite values up to 3.4028235e38 (7F7FFFFF). A larger number rounds to infinity, 7F800000, and the converter shows a warning because the number you typed is not what is stored. At the small end, the smallest normal value is 1.17549435e-38; below that the number becomes a subnormal with fewer digits, so 1e-40 is stored as 000116C2 and reads back as about 9.99995e-41. Under about 1.4e-45 everything rounds to zero.
| Typed | Stored as | Remark |
|---|---|---|
| 3.4028235e38 | 7F7FFFFF | largest finite single |
| 3.5e38 | 7F800000 | overflows to infinity |
| 1.17549435e-38 | 00800000 | smallest normal single |
| 1e-40 | 000116C2 | subnormal, reduced precision |
| 1e-45 | 00000001 | smallest subnormal single |
| 1e-46 | 00000000 | underflows to zero |
Doubles reach 1.7976931348623157e308 and down to 5e-324, so most values that overflow a single are ordinary doubles. Switch the precision to 64-bit before assuming the number is out of range.
Single or double: which precision to write
Write the precision the reader expects, since the two patterns are different lengths and not interchangeable. Single precision is common in graphics, audio, sensor firmware and file formats that favour compact storage. Double precision is what JavaScript numbers and Python floats are, and what most scientific code and spreadsheets use. If you control the format, a double avoids the visible seventh-digit error and costs four extra bytes.
Byte order when you write the bytes out
The hex shown by default is the number written from the most significant byte, which is how a reference would print it. A file or a memory image on a PC stores the same number little-endian, so 3.14159274 is DB 0F 49 40 when written to memory and 40 49 0F DB when sent in network order. Tick Little-endian bytes to get the memory order, and check which one the receiving format requires; a binary file specification states it for each field.
| Number | Big-endian | Little-endian |
|---|---|---|
| 1.5 | 3F C0 00 00 | 00 00 C0 3F |
| 3.14159274 | 40 49 0F DB | DB 0F 49 40 |
| -2 | C0 00 00 00 | 00 00 00 C0 |
Splitting a float into two 16-bit registers
Some device protocols, notably Modbus, carry a 32-bit float in two 16-bit registers. Encode the value, then cut the eight hex digits into two groups of four. For 123.45 the single is 42F6E666, which gives the registers 42F6 and E666. Which register holds the high half is a device convention, so use the byte and word order from the device manual and test with a known value before writing to a production system.
NaN, infinity and negative zero as hex
The converter accepts the words NaN and Infinity, and a minus sign for the negative forms. The results are fixed patterns with the exponent field set to all ones.
| Typed | Single | Double |
|---|---|---|
| Infinity | 7F800000 | 7FF0000000000000 |
| -Infinity | FF800000 | FFF0000000000000 |
| NaN | 7FC00000 | 7FF8000000000000 |
| -0 | 80000000 | 8000000000000000 |
The NaN produced here is the quiet NaN that the language runtime creates. Other NaN patterns exist, with a different mantissa, and most programs treat them all alike. If you need an exact NaN payload, write the hex directly and decode it with hex to float.
Float to hex in JavaScript and PowerShell
In JavaScript, a DataView writes a number as a float and reads the bytes back as an integer. In PowerShell, BitConverter returns the bytes in the machine's order, little-endian on Windows. The first line below prints 3DCCCCCD for 0.1; see DataView on MDN.
const view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, 0.1);
console.log(view.getUint32(0).toString(16).toUpperCase()); // 3DCCCCCD
const d = new DataView(new ArrayBuffer(8));
d.setFloat64(0, 0.1);
console.log(d.getBigUint64(0).toString(16).toUpperCase()); // 3FB999999999999A[BitConverter]::GetBytes([single]0.1) | ForEach-Object { $_.ToString('X2') } # CD CC CC 3D (memory order)
[BitConverter]::ToString([BitConverter]::GetBytes([double]0.1)) # 9A-99-99-99-99-99-B9-3FChecking a result by decoding it again
A reliable test of any encoding is to decode it and compare. Paste the hex into hex to float with the same precision and byte order, and the value should come back as the shortest decimal that matches what you typed, for example 0.1 from 3DCCCCCD. If the decoded text differs in the seventh digit for a single, that is normal rounding. If it differs in the first digit, the byte order or the width is wrong.
What you can type, and what the converter rejects
Plain decimals (3.14, -0.5), exponent notation (1e-7, 6.02E23), a leading or trailing dot, and underscores as digit separators (1_000.5) are read as numbers. A comma is treated as the decimal point, so 1,5 is 1.5 and a thousands separator is not supported; write 1000.5, not 1,000.5. Words other than NaN and Infinity, hex input, units and more than one decimal point are rejected with a message instead of being guessed.
Only the 32-bit and 64-bit formats are offered, because those are the ones programming languages and file formats store natively. Half precision (16 bits) and the 80-bit or 128-bit extended formats have other field sizes and are not produced here. When a source does not say which width it uses, the length of its hex tells you: 8 digits for a single and 16 for a double.
Questions about converting a float to hex
What is 1.0 as a float in hex?
3F800000 in single precision and 3FF0000000000000 in double precision. Both have a zero mantissa; only the exponent field differs, 127 in one and 1023 in the other.
Why does 0.1 end in CD in single precision?
Because 0.1 is a repeating fraction in binary, so the 23-bit mantissa is rounded, and the rounding pushes the last hex digits from CC to CD. The stored value is 0.100000001490116119384765625, slightly above 0.1.
Is a negative float the two's complement of the positive one?
No. A float is sign and magnitude: only the top bit changes. 2.0 is 40000000 and -2.0 is C0000000. Two's complement applies to integers, as on the decimal to hex page.
What happens if my number does not fit in 32 bits?
Above about 3.4028235e38 it is stored as infinity, 7F800000, and the converter warns you. Below about 1.4e-45 it becomes zero. Between 1.17549435e-38 and 1.4e-45 it is stored as a subnormal with fewer significant digits.
Which byte order should I use?
Use the one the receiving system reads. Memory on a PC and most files from it are little-endian, so 3.14159274 is DB 0F 49 40. Network protocols commonly send big-endian, 40 49 0F DB. When in doubt, check the specification of the format.
