Thread

Zero-JS Hypermedia Browser

Relays: 5
Replies: 0
Generated: 15:49:06
Parman's #nerdtips Do you want to know why a lot of base64 numbers (not all) end with = or ==? eg dGhlIHNhbXBsZSBub25jZQ== This might seem boring, but explaining it covers a lot of interesting things (also, I'm assuming you know what base64 is, but I will take requests to explain that another time). The = sign is not part of the character set (64 characters) of base64. It's used for padding at the end (for remainders), when the data to be encoded doesn't fit evenly in the byte-groups that will be used. It starts with understanding that base 64 encodes data with 6 bits at a time (00000000-00111111 in binary can hold the values 0 to 63 in decimal), and the smallest data communication unit in modern computers, a byte, has 8 bits (8 bits can hold 0 to 255, ie 256 different numbers). So instead of wasting two leading bits per byte, the lowest common multiple of 6 and 8 is used as a minimum "packet" for base64 storage - that common multiple is 24. So 3 bytes (24 bits) will correspond to four base64 characters. But, if the number of characters needed doesn't divide evenly, ie, there is a remainder, then the = sign indicates whether there is one or two entire unused bytes at the end. For example, let's say there is a large number (all data on a computer is a number) that requires 21 base64 characters to encode. That would use up 3 bytes for each 4 base64 characters (4 characters fit per 3 bytes), so 5 groups of 3 bytes (15 bytes) will be used for the first 20 characters. The last character will sit inside a 3-byte group on its own. The first byte of the three will have 6 bits with two unused bits, and then there are two entire unused bytes, so two ='s are added at the end of the base64 number. BUT WHY? Why not just print the base64 number and omit the = sign(s)? Because their purpose isn't for encoding the number - the purpose is for the time when the number is going to be decoded back to bytes. It is done in groups of 3 bytes, and without the = sign, the decoder won't know if there is a remainder or not, and the empty bytes will be interpreted as a zero instead of padding. https://armantheparman.com/nerdtips
2025-11-07 06:46:47 from 1 relay(s)
Login to reply