Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with encoding.utf8.getbytes in c#

Tags:

c#

I am working on C#, trying below code

byte[] buffer = new byte[str.Length];
buffer = Encoding.UTF8.GetBytes(str);

In str I've got lengthy data but I've got problem in getting complete encoded bytes. Please tell me what's going wrong and how can I overcome this problem?

like image 792
MGK Avatar asked Apr 20 '26 19:04

MGK


1 Answers

Why are you creating a new byte array and then ignoring it? The value of buffer before the call to GetBytes is being replaced with a reference to a new byte array returned by GetBytes.

However, you shouldn't expect the UTF-8 encoded version of a string to be the same length in bytes as the original string's length in characters, unless it's all ASCII. Any character over U+007F takes up at least 2 bytes.

What's the bigger picture here? What are you trying to achieve, and why does the length of the byte array matter to you?

like image 87
Jon Skeet Avatar answered Apr 23 '26 08:04

Jon Skeet