Questions tagged [base64]

Base64 is a set of encoding schemes that represent binary data in an ASCII string format.

Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Base64 term originates from a specific MIME content transfer encoding.

Base64 uses characters 0 to 9, A to Z and a to z to represent digits of 64-decimal numbers. The last two required digits differ between schemas. For instance, MIME (RFC 2045) uses + and / , XML names tokens use - and ., XML identifiers use _ and : and regular expressions use ! and -.

When encoding in Base64 text, 3 bytes are typically encoded into 4 characters. To encode arbitrary lengths, the padding character (=) is used. = at the end of encoded sequence means that only two bytes and == means that only one byte is encoded by the last 4 character group.

Characters outside the discussed alphabet are normally forbidden, except in MIME where they are discarded.

Base64 encoding schemes are commonly used when there is a need to encode binary data that need to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, or to store binary data in textual formats such as XML or JSON.

Useful online tools

They work without enabled JavaScript too.

11149 questions
243
votes
6 answers

Encoding an image file with base64

I want to encode an image into a string using the base64 module. I've ran into a problem though. How do I specify the image I want to be encoded? I tried using the directory to the image, but that simply leads to the directory being encoded. I want…
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
236
votes
10 answers

How to convert a Base64 string into a Bitmap image to show it in a ImageView?

I have a Base64 String that represents a BitMap image. I need to transform that String into a BitMap image again to use it on a ImageView in my Android app How to do it? This is the code that I use to transform the image into the base64 String:…
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
235
votes
19 answers

How do I do base64 encoding on iOS?

I'd like to do base64 encoding and decoding, but I could not find any support from the iPhone SDK. How can I do base64 encoding and decoding with or without a library?
BlueDolphin
  • 9,765
  • 20
  • 59
  • 74
234
votes
10 answers

How can I save a base64-encoded image to disk?

My Express app is receiving a base64-encoded PNG from the browser (generated from canvas with toDataURL() ) and writing it to a file. But the file isn't a valid image file, and the "file" utility simply identifies it as "data". var body =…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
220
votes
16 answers

Base64 length calculation?

After reading the base64 wiki ... I'm trying to figure out how's the formula working : Given a string with length of n , the base64 length will be Which is : 4*Math.Ceiling(((double)s.Length/3))) I already know that base64 length must be %4==0…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
208
votes
6 answers

Base64: What is the worst possible increase in space usage?

If a server received a base64 string and wanted to check it's length before converting,, say it wanted to always permit the final byte array to be 16KB. How big could a 16KB byte array possibly become when converted to a Base64 string (assuming one…
700 Software
  • 85,281
  • 83
  • 234
  • 341
201
votes
10 answers

Convert base64 string to ArrayBuffer

I need to convert a base64 encode string into an ArrayBuffer. The base64 strings are user input, they will be copy and pasted from an email, so they're not there when the page is loaded. I would like to do this in javascript without making an ajax…
Tony
  • 2,043
  • 2
  • 12
  • 5
192
votes
22 answers

Python: Ignore 'Incorrect padding' error when base64 decoding

I have some data that is base64 encoded that I want to convert back to binary even if there is a padding error in it. If I use base64.decodestring(b64_string) it raises an 'Incorrect padding' error. Is there another way? UPDATE: Thanks for all the…
FunLovinCoder
  • 7,597
  • 11
  • 46
  • 57
190
votes
6 answers

Strange \n in base64 encoded string in Ruby

The inbuilt Base64 library in Ruby is adding some '\n's. I'm unable to find out the reason. For this special example: irb(main):001:0> require 'rubygems' => true irb(main):002:0> require 'base64' => true irb(main):003:0> str = …
intellidiot
  • 11,108
  • 4
  • 34
  • 41
187
votes
6 answers

Base64 Java encode and decode a string

I want to encode a string into base64 and transfer it through a socket and decode it back. But after decoding it gives different answer. Following is my code and result is "77+9x6s=" import javax.xml.bind.DatatypeConverter; public class f{ …
Sameera Kumarasingha
  • 2,908
  • 3
  • 25
  • 41
186
votes
5 answers

Why does base64 encoding require padding if the input length is not divisible by 3?

What is the purpose of padding in base64 encoding. The following is the extract from wikipedia: "An additional pad character is allocated which may be used to force the encoded output into an integer multiple of 4 characters (or equivalently when…
Anand Patel
  • 6,031
  • 11
  • 48
  • 67
186
votes
10 answers

Get data from file input in JQuery

I actually have a file input and I would like to retrieve the Base64 data of the file. I tried: $('input#myInput')[0].files[0] to retrieve the data. But it only provides the name, the length, the content type but not the data itself. I actually…
kschaeffler
  • 4,083
  • 7
  • 33
  • 41
182
votes
8 answers

Convert Base64 string to an image file?

I am trying to convert my base64 image string to an image file. This is my Base64 string: http://pastebin.com/ENkTrGNG Using following code to convert it into an image file: function base64_to_jpeg( $base64_string, $output_file ) { $ifp = fopen(…
Badal
  • 3,738
  • 4
  • 33
  • 60
172
votes
6 answers

What is the purpose of base 64 encoding and why it used in HTTP Basic Authentication?

I don't get the Base64 encryption. If one can decrypt a Base64 string, what is it's purpose? Why is it being used for HTTP Basic auth? It's like telling to someone my password is reversed into OLLEH. People seeing OLLEH will know the original…
ajsie
  • 77,632
  • 106
  • 276
  • 381
171
votes
20 answers

How to check for a valid Base64 encoded string

Is there a way in C# to see if a string is Base 64 encoded other than just trying to convert it and see if there is an error? I have code code like this: // Convert base64-encoded hash value into a byte array. byte[] HashBytes =…
Chris Mullins
  • 6,677
  • 2
  • 31
  • 40