To prevent 'InvalidCharacterError' error, you need to do this:
var base64EncodedStr = btoa(unescape(encodeURIComponent(rawData)));
Answer from Frane Poljak on Stack OverflowTo prevent 'InvalidCharacterError' error, you need to do this:
var base64EncodedStr = btoa(unescape(encodeURIComponent(rawData)));
Use a FileReader to encode your image as a data URL:
jQuery.ajax({...})
.done(function (r) {
var reader = new FileReader(
reader.onload = (function(self) {
return function(e) {
document.getElementById("img").src = e.target.result;
}
})(this);
reader.readAsDataURL(new Blob([r]));
});
The problem of using those online tools isn't going to be in the base64 conversion - it's going to be parsing the hex string into a byte array to start with. In your real code that won't be a problem, as it'll be the output of another stage. Just to prove that, here's some sample Java code, using a public domain base64 encoder:
public class Test {
public static void main(String[] args) throws Exception {
byte[] data = { (byte) 0xB6, (byte) 0x79, (byte) 0xC0, (byte) 0xAF,
(byte) 0x18, (byte) 0xF4, (byte) 0xE9, (byte) 0xC5,
(byte) 0x87, (byte) 0xAB, (byte) 0x8E, (byte) 0x20,
(byte) 0x0A, (byte) 0xCD, (byte) 0x4E, (byte) 0x48,
(byte) 0xA9, (byte) 0x3F, (byte) 0x8C, (byte) 0xB6 };
String text = Base64.encodeBytes(data);
System.out.println(text);
}
}
Output: tnnArxj06cWHq44gCs1OSKk/jLY=
Java 8 has come up with java.util.Base64 package to help encode bytes to Base64 and
vice-versa
import java.util.Base64;
byte[] bytes = string.getBytes();
// returns byte[]
Base64.getEncoder().encode(bytes);
// returns String
Base64.getEncoder().encodeToString(bytes);
Depending on how you represent the value 0b100
>>> import struct
>>> val = 0b100
>>> print struct.pack('I', val).encode('base64')
BAAAAA==
This will turn your value into a 4-byte integer in native endianness, and encode that value into base64. You need to specify the width of your data as base64 is really made for representing binary data in printable characters.
You can typically encode data as a string of bytes via struct's functions, and then encode into base64. Ensure you're using the same data layout and endianess when decoding.
Binary literals are just normal python integers.
>>> 0b100
<<< 4
>>> 4
<<< 4
Just convert to a string and base64 encode it (giving you a string that is the base64 representation of '4'). There is nothing wrong with this approach, it's lossless and simple.
>>> s = str(0b100).encode('base64')
>>> int(s.decode('base64'))
<<< 4
If you want, you can use bin to convert the int into a binary string:
>>> bin(int(s.decode('base64')))
<<< '0b100'
I have solved this issue by implementing a .net component for converting to base64 string. The hard part is the binary data sent to the .net COM from ASP is received as a string. Convert.ToBase64() accepts only byte[]. So I tried converting string to byte[].
But the encoding available in .net (Unicode, ASCII, UTF) doesn't works fine. There are data loss, while these encodings are used. Finally I get it done by using StringReader object. Read char by char(16 bit) and converted them to (8 bit) byte[] array.
And the performance is best.
Regards, Siva.
you should use the .NET methods Convert.ToBase64String and Convert.FromBase64String.
Use the Convert.FromBase64String( ) method. This will give you the binary data back (as a byte array).
To convert binary data to a Base64 string see conversion functions from binary data to a string in vbscript
from http://www.motobit.com/tips/detpg_Base64Encode/
Function Base64EncodeBinary(inData)
Base64EncodeBinary = Base64Encode(BinaryToString(inData))
End Function
Function Base64Encode(inData)
'rfc1521
'2001 Antonin Foller, Motobit Software, http://Motobit.cz
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim cOut, sOut, I
'For each group of 3 bytes
For I = 1 To Len(inData) Step 3
Dim nGroup, pOut, sGroup
'Create one long from this 3 bytes.
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
&H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
'Oct splits the long To 8 groups with 3 bits
nGroup = Oct(nGroup)
'Add leading zeros
nGroup = String(8 - Len(nGroup), "0") & nGroup
'Convert To base64
pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
'Add the part To OutPut string
sOut = sOut + pOut
'Add a new line For Each 76 chars In dest (76*3/4 = 57)
'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
Next
Select Case Len(inData) Mod 3
Case 1: '8 bit final
sOut = Left(sOut, Len(sOut) - 2) + "=="
Case 2: '16 bit final
sOut = Left(sOut, Len(sOut) - 1) + "="
End Select
Base64Encode = sOut
End Function
Function MyASC(OneChar)
If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function