Get encoded message length in bytes without creating a C # object

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 don't need to use an array, just the number of bytes). Is it possible? My question is almost exactly this , but with C # instead of Java.

Thanks!

+5
source share
1 answer

You can use the GetByteCount method to get the number of bytes that the string will create with the given encoding.

var byteCount = System.Text.Encoding.UTF8.GetByteCount("myString"); 
+7
source

Source: https://habr.com/ru/post/1274620/


All Articles