user2545071
user2545071

Reputation: 1410

How to convert file to base64 UTF-8 little endian

Good day!

I convert binary file into char array:

var bytes = File.ReadAllBytes(@"file.wav");
char[] outArr = new char[(int)(Math.Ceiling((double)bytes.Length / 3) * 4)];
var result = Convert.ToBase64CharArray(bytes, 0, bytes.Length, outArr, 0,  Base64FormattingOptions.None);

string resStr = new string(outArr);

So, is it little endian? And does it convert to UTF-8?

Thank you!

Upvotes: 0

Views: 3571

Answers (2)

David Heffernan
David Heffernan

Reputation: 613412

  • C# char represents a UTF-16 character element. So there is no UTF-8 here.
  • Since .net is little endian, and since char is two bytes wide, then the char array, and the string, are both stored in little endian byte order.

If you want to convert your byte array to base64 and then encode as UTF-8 do it like this:

byte[] base64utf8 = Encoding.UTF8.GetBytes(Convert.ToBase64String(bytes));

If you wish to save the base64 text to a file, encoded as UTF-8, you could do that like so:

File.WriteAllText(filename, Convert.ToBase64String(bytes), Encoding.UTF8);

Since UTF-8 is a byte oriented encoding, endianness is not an issue.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502835

You don't have any UTF-8 here - and UTF-8 doesn't have an endianness anyway, as its code unit size is just a single byte.

Your code would be simpler as:

var bytes = File.ReadAllBytes(@"file.wav");
string base64 = Convert.ToBase64String(bytes);

If you then write the string to a file, that would have an encoding, which could easily be UTF-8 (and will be by default), but again there's no endianness to worry about.

Note that as base64 text is always in ASCII, each character within a base64 string will take up a single byte in UTF-8 anyway. Even if UTF-8 did have different representations for multi-byte values, it wouldn't be an issue here.

Upvotes: 4

Related Questions