Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTF-8 to base64 string

I'm trying to convert UTF-8 to base64 string.

Example: I have "abcdef==" in UTF-8. It's in fact a "representation" of a base64 string.

How can I retrieve a "abcdef==" base64 string (note that I don't want a "abcdef==" "translation" from UTF-8, I want to get a string encoded in base64 which is "abcdef==").

EDIT
As my question seems to be unclear, here is a reformulation:

My byte array (let's say I name it A) is represented by a base64 string. Converting A to base64 gives me "abcdef==".

This string representation is sent through a socket in UTF-8 (note that the string representation is exactly the same in UTF-8 and base64). So I receive an UTF-8 message which contains "whatever/abcdef==/whatever" in UTF-8.

So I need to retrieve the base64 "abcedf==" string from this socket message in order to get A.

I hope this is more clear!

like image 453
Nicolas Voron Avatar asked Dec 18 '12 17:12

Nicolas Voron


People also ask

How do I get Base64 encoded strings?

In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string("atob" should be read as "ASCII to binary").

Is Base64 same as UTF-8?

UTF-8 is like the other UTF encodings a character encoding to encode characters of the Unicode character set UCS. Base64 is an encoding to represent any byte sequence by a sequence of printable characters (i.e. A – Z , a – z , 0 – 9 , + , and / ). There is no System. Text.

What is Base64 string example?

Base64 Encoding Example A Base64 encoder starts by chunking the binary stream into groupings of six characters: 100110 111010 001011 101001. Each of these groupings translates into the numbers 38, 58, 11, and 41.

What is Base64 string?

In computer programming, Base64 is a group of binary-to-text encoding schemes that represent binary data (more specifically, a sequence of 8-bit bytes) in sequences of 24 bits that can be represented by four 6-bit Base64 digits.


1 Answers

It's a little difficult to tell what you're trying to achieve, but assuming you're trying to get a Base64 string that when decoded is abcdef==, the following should work:

byte[] bytes = Encoding.UTF8.GetBytes("abcdef=="); string base64 = Convert.ToBase64String(bytes); Console.WriteLine(base64); 

This will output: YWJjZGVmPT0= which is abcdef== encoded in Base64.

Edit:

To decode a Base64 string, simply use Convert.FromBase64String(). E.g.

string base64 = "YWJjZGVmPT0="; byte[] bytes = Convert.FromBase64String(base64); 

At this point, bytes will be a byte[] (not a string). If we know that the byte array represents a string in UTF8, then it can be converted back to the string form using:

string str = Encoding.UTF8.GetString(bytes); Console.WriteLine(str); 

This will output the original input string, abcdef== in this case.

like image 163
Iridium Avatar answered Sep 17 '22 14:09

Iridium