Introduction
Base64 encoding is encoding binary data to string format by
translating into using a radix-64 representation.
Contents
- How to encode a string into Base64
- How to encode large files into Base64 using c#
How to encode a string into Base64
Let us
encode a string “ab” into base64
Sring
|
a
|
b
|
||||||||||||||
ASCII
|
91
|
92
|
||||||||||||||
Bit wise
|
0
|
1
|
0
|
1
|
1
|
0
|
1
|
1
|
0
|
1
|
0
|
1
|
1
|
1
|
0
|
0
|
Grouping Bits (6 bits into one group)
|
6 bits
|
6bits
|
4bits
|
|||||||||||||
ASCII
|
22
|
53
|
12
|
|||||||||||||
Base64 Encode
|
W
|
1
|
M
|
|||||||||||||
Base64 Char set
Value
|
Char
|
Value
|
Char
|
Value
|
Char
|
Value
|
Char
|
Value
|
Char
|
Value
|
Char
|
0
|
A
|
11
|
L
|
22
|
W
|
33
|
h
|
44
|
s
|
55
|
3
|
1
|
B
|
12
|
M
|
23
|
X
|
34
|
i
|
45
|
t
|
56
|
4
|
2
|
C
|
13
|
N
|
24
|
Y
|
35
|
j
|
46
|
u
|
57
|
5
|
3
|
D
|
14
|
O
|
25
|
Z
|
36
|
k
|
47
|
v
|
58
|
6
|
4
|
E
|
15
|
P
|
26
|
a
|
37
|
l
|
48
|
w
|
59
|
7
|
5
|
F
|
16
|
Q
|
27
|
b
|
38
|
m
|
49
|
x
|
60
|
8
|
6
|
G
|
17
|
R
|
28
|
c
|
39
|
n
|
50
|
y
|
61
|
9
|
7
|
H
|
18
|
S
|
29
|
d
|
40
|
o
|
51
|
z
|
62
|
+
|
8
|
I
|
19
|
T
|
30
|
e
|
41
|
p
|
52
|
0
|
63
|
/
|
9
|
J
|
20
|
U
|
31
|
f
|
42
|
q
|
53
|
1
|
|
|
10
|
K
|
21
|
V
|
32
|
g
|
43
|
r
|
54
|
2
|
|
|
How to encode large files into Base64 using c#
.NET has a built-in function to convert a string into Base64
Encoded string. But it may throw an exception (like out of memory) when you are
encoding huge data. I am going to create a function which can handle any size
file in encoding data to Base64 encoded string
public class Base64Encoder
{
const int SegmentSize
= 20 * 1024;
public void Encode(string sourceFile, string
destFile)
{
/* Reading a file by segmentwise and encode into Base64string and
then write into file During
Base64Encoding, 3 characters(3 bytes) will be converted into 4 characters(4
bytes). Hence we need to take segment of size multiples of 3
*/
long actualSegmentSize = 3 * SegmentSize;
long fileLength=0;
using (FileStream
sourceStream = new FileStream(sourceFile,
FileMode.Open, FileAccess.Read))
{
if (File.Exists(destFile))
File.Delete(destFile);
FileStream
destStream = new FileStream(destFile,
FileMode.Create, FileAccess.Write);
long filePosition = 0;
fileLength = sourceStream.Length;
byte[] sourceSegment;
byte[] base64EncodeSegment;
while (filePosition < fileLength)
{
if (filePosition + actualSegmentSize
> fileLength)
sourceSegment = new byte[fileLength];
else
sourceSegment = new byte[actualSegmentSize];
sourceStream.Read(sourceSegment, 0, sourceSegment.Length);
base64EncodeSegment
= System.Text.ASCIIEncoding.ASCII.GetBytes(Convert.ToBase64String(sourceSegment));
destStream.Write(base64EncodeSegment, 0, base64EncodeSegment.Length);
filePosition = sourceStream.Position;
}
destStream.Close();
}
}
public void Decode(string sourceFile, string
destFile)
{
/* Reading Base64Encoded file by segmentwise and decode into
string and then write into file During
Base64Encoding, 3 characters(3 bytes) will be converted into 4 characters(4
bytes). Hence we need to take segment of size multiples of 4 for decoding
*/
long actualSegmentSize = 4 * SegmentSize;
long fileLength = 0;
using (FileStream sourceStream = new
FileStream(sourceFile, FileMode.Open, FileAccess.Read))
{
if (File.Exists(destFile))
File.Delete(destFile);
FileStream
destStream = new FileStream(destFile,
FileMode.Create, FileAccess.Write);
long filePosition = 0;
fileLength = sourceStream.Length;
byte[] sourceSegment;
byte[] base64EncodeSegment;
while (filePosition < fileLength)
{
if (filePosition + actualSegmentSize
> fileLength)
sourceSegment = new byte[fileLength];
else
sourceSegment = new byte[actualSegmentSize];
sourceStream.Read(sourceSegment, 0, sourceSegment.Length);
base64EncodeSegment
= Convert.FromBase64String(System.Text.ASCIIEncoding.ASCII.GetString(sourceSegment));
destStream.Write(base64EncodeSegment, 0, base64EncodeSegment.Length);
filePosition = sourceStream.Position;
}
destStream.Close();
}
}
}
when converting more than 700 mb,there was outofmemoryexception. how to convert larger files to base64 and return the string without outofmemoryexception?
ReplyDelete