user1111699
user1111699

Reputation: 15

how to decode string in base64 and keep it as string in c# asp.net4

I am try to write this stuff in c# asp.net 4 framework. I have a String variable called u. it is base64 encode of another string. now I need to decode it and get the original string, the problem is that decoding convert it to byte array.how can I convert it to string?

I tried Convert.FromBase64String(u).toString() but it didn't help :(

Upvotes: 1

Views: 8509

Answers (1)

Peter Ritchie
Peter Ritchie

Reputation: 35881

Base-64 could be an encoding of anything. If you know it's an encoding of an ASCII string, you could do this:

var bytes = Convert.FromBase64String(u);
var text = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);

If it's an encoding of a Unicode string (you weren't specific), you could do this:

var bytes = Convert.FromBase64String(u);
var text = System.Text.Encoding.Unicode.GetString(bytes, 0, bytes.Length);

Upvotes: 2

Related Questions