R
R
root092016-01-27 17:40:11
Character encoding
root09, 2016-01-27 17:40:11

Convert string to base64?

What could be the problem?
There is a task to convert a string to base64,
For example, the string " {"response":"2,4,5"} " should become " eyJyZXNwb25zZSI6IjIsNCw1In0. "
I'm trying to convert the following code:

public static string Base64Encode(string plainText)
{
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
}

But at the output I get " eyJyZXNwb25zZSI6IjIsNCw1In0= "
If this is decoded back to a string, then it will also be " {"response":"2,4,5"} ", but why does the hash look different? how to get " eyJyZXNwb25zZSI6IjIsNCw1In0. " from the string " {"response":"2,4,5"} " ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-01-27
@petermzg

You have correctly converted.
"=" special suffix code
From wikipedia:
"If only one or two bytes are encoded, the result is only the first two or three characters of the string, and the output string is padded with two or one "=" characters. This prevents extra bits from being added to the recovered data. "

A
aminought, 2016-01-27
@aminought

What makes you think that you need exactly "eyJyZXNwb25zZSI6IjIsNCw1In0."? In my opinion, everything is correct. If you are confused by the hash, then look in a hex editor to see what is wrong with the decoded string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question