简体   繁体   中英

How many bytes does a null-terminator take based on a chosen encoding?

Encoding.GetByteCount(String) tells me how many bytes it will take to represent a given C# string in a byte[] based on a chosen encoding but an external protocol demands strings are null-terminated and that the total size of the message containing the string is sent in a header.

Is calculating the size of my char[] as simple as adding 1 byte to the result of GetByteCount(String) , or might my NULL-terminator be different size based on encoding too? If so how can I determine this?

I am asking in the general case, not about a specific encoding.

eg int HowManyNullBytesToAdd(Encoding encoding)

For all commonly used encodings, the simple answer to your question is

MyEncoding.GetByteCount("\0")

However, if you want to consider full generality then you might consider arbitrary encodings, perhaps not even part of any standard, encodings that you or I can invent. And then all bets are off. In that full generality, then I think you'd need to know more about the specific encoding.

However, for the most commonly used Unicode encodings, UTF-8, UTF-16 and UTF-32, the simple code above is vaiid.

Byte count to represent your string in bytes depends on the encoding the receiver expects. so following may give not the same results if your string contains non-ascii characters.

var byteCountASCII = System.Text.Encoding.ASCII.GetByteCount(yourCharArray);
var byteCountUTF8 = System.Text.Encoding.UTF8.GetByteCount(yourCharArray);

If it is ASCII you can safely add 1 to it for null character but as far as i know for example UTF16 uses 2 bytes even for simple ascii characters so that depends also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM