开发者

Bit/byte conversion

How many bits is a 开发者_如何转开发.NET string that's 10 characters in length? (.NET strings are UTF-16, right?)


On 32-bit systems:

4 bytes          = Type pointer (Every object has one of these)
4 bytes          = Lock         (One of these too!)
4 bytes          = Length       (Need the length)
2 * Length bytes = Data         (And the chars themselves)
=======================
12 + 2*Length bytes
=======================
96 + 16*Length bits

So 10 chars would = 256 bits = 32 bytes

I am not sure if the Lock grows to 64-bit on 64-bit systems. I kinda hope not, but you never know. The 64-bit structure overhead is therefore anywhere from 16-20 bytes (as opposed to the 12 bytes on 32-bit).


Every char in the string is two bytes in size, so if you are just converting the chars directly and not using any particular encoding, the answer is string.Length * 2 * 8

otherwise the result depends on the encoding, you can write:

int numbits = System.Text.Encoding.UTF8.GetByteCount(str)*8; //returns 80

or

int numbits = System.Text.Encoding.Unicode.GetByteCount(str)*8 //returns 160


If you are talking pure Unicode-16 then:

10 characters = 20 bytes = 160 bits

This really needs a context in order to be answered properly.


It all comes down to how you define character and how to you store the data.

For example, if you define character as a single letter from the users point of view it can be more than 2 bytes, for example this character: Å is two Unicode code points (U+0041 U+030A, Latin Capital A + Combining Ring Above) so it will require two .net chars or 4 bytes int UTF-16.

Now even if you are talking about 10 .net Char elements than if it's in memory you have some object overhead (that was already mentioned) and a bit of alignment overhead (on 32bit system everything has to be aligned to 4 bytes boundary, in 64bit the rules are more complicated) so you may have some empty bytes at the end.

If you are talking about database or files than each database and file system has its own overhead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜