Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get length of encoded message in bytes without creating the object C#

Tags:

c#

I would like to get the number of bytes in a string using UTF-8 encoding without explicitly creating an array of bytes (because I do not need to use the array, just the number of bytes). Is this possible? My question is almost exactly this one but with C# instead of Java.

Thanks!

like image 263
Walker in the City Avatar asked Jan 10 '18 17:01

Walker in the City


1 Answers

You can use the method GetByteCount to get the number of bytes that the string would produce with a given encoding.

var byteCount = System.Text.Encoding.UTF8.GetByteCount("myString");
like image 132
Jehof Avatar answered Nov 10 '22 03:11

Jehof