How to convert Base64 to a byte array
- Paste the Base64 into the input. Whitespace, missing padding, URL-safe characters and a
data:prefix are accepted. - Pick the target under the output with Language: C / C++, JavaScript, Python, Java, Go, C# or Rust. JavaScript is selected when the page opens.
- Copy output and paste the declaration into your code. Rename
datato suit. - The From and To menus above the tool switch the input or output format, for example to read hex instead of Base64.
To go back from an array to Base64, use Byte Array to Base64, which reads all of these formats.
What each language gets
The Base64 3q2+7wABAgOAf/8K decodes to 12 bytes, de ad be ef 00 01 02 03 80 7f ff 0a. This is the output for each Language setting:
const unsigned char data[12] = {
0xde, 0xad, 0xbe, 0xef, 0x00, 0x01, 0x02, 0x03, 0x80, 0x7f, 0xff, 0x0a
};
const data = new Uint8Array([
0xde, 0xad, 0xbe, 0xef, 0x00, 0x01, 0x02, 0x03, 0x80, 0x7f, 0xff, 0x0a
]);
data = b'\xde\xad\xbe\xef\x00\x01\x02\x03\x80\x7f\xff\x0a'
byte[] data = new byte[] {
-34, -83, -66, -17, 0, 1, 2, 3, -128, 127, -1, 10
};
data := []byte{
0xde, 0xad, 0xbe, 0xef, 0x00, 0x01, 0x02, 0x03, 0x80, 0x7f, 0xff, 0x0a,
}
byte[] data = new byte[] {
0xde, 0xad, 0xbe, 0xef, 0x00, 0x01, 0x02, 0x03, 0x80, 0x7f, 0xff, 0x0a
};
let data: [u8; 12] = [
0xde, 0xad, 0xbe, 0xef, 0x00, 0x01, 0x02, 0x03, 0x80, 0x7f, 0xff, 0x0a
];
Notes on the output:
- Arrays wrap after 12 values per line. Python writes 16 bytes per
b'...'string and joins longer data inside parentheses, which Python concatenates at compile time. - Java uses signed decimals because
(byte) 0xdewould need a cast on every value above0x7f.-34and0xdeare the same bits. - C and Rust declare the length. The C array is
unsigned char, so values above 127 fit without warnings. - Go gets a trailing comma after the last value. The compiler requires it when the closing brace is on its own line.
- Python writes every byte as
\x.., even printable ones, so the literal is unambiguous to read and to diff.
Embed the bytes, or keep the Base64?
A byte array literal makes sense when the program has no Base64 decoder or should not spend time decoding: firmware for microcontrollers, test fixtures, a small key or certificate compiled into a binary, a lookup table. The cost is size in the source file. Each byte takes about six characters (0xde, ), more than four times the Base64, so 10 KB of data becomes roughly 60 KB of code.
For anything that changes or is larger than a few kilobytes, keep the Base64 string and decode at startup:
import base64
data = base64.b64decode("3q2+7wABAgOAf/8K")
byte[] data = Convert.FromBase64String("3q2+7wABAgOAf/8K");
byte[] data = java.util.Base64.getDecoder().decode("3q2+7wABAgOAf/8K");
const data = Uint8Array.from(atob("3q2+7wABAgOAf/8K"), c => c.charCodeAt(0));
Newer browsers also have Uint8Array.fromBase64(), covered in Uint8Array.fromBase64 and toBase64. The language pages for C#, Java, Go, Rust and Python show the decoders in more detail.
Common problems
- “Not valid Base64”. The string contains a character outside the alphabet or was cut off. The message gives the position; the Base64 validator explains what is wrong with a broken string.
- “Narrowing conversion” errors in C++.
char data[] = {0xde};does not compile as C++11 or later, because plaincharis signed on most platforms and 222 does not fit. Keep the generatedunsigned char, or useuint8_t. - Huge output. For large files,
xxd -i file.binprints a C array directly, and embedding tools such as C23#embedavoid the array altogether.
FAQ
How do I convert a Base64 string to a byte array in C#?
At runtime, byte[] data = Convert.FromBase64String(s);. To hard-code the bytes instead, set Language to C# here and paste the new byte[] { ... } declaration into your source.
Why does the Java array contain negative numbers?
Java’s byte is signed and holds −128 to 127. Bytes from 0x80 to 0xff are written as −128 to −1, so 0xde appears as -34. The bits are the same; b & 0xff gives the unsigned value back.
How do I turn Base64 into bytes in Python?
base64.b64decode(s) returns a bytes object. Set Language to Python here if you want that value written out as a b'\x..' literal.
Is the JavaScript output a Uint8Array?
Yes, const data = new Uint8Array([...]) with hex values. It runs in browsers and Node.js; in Node you can wrap it with Buffer.from(data) if an API needs a Buffer.