How do I find out how many bytes a character has?

有些话、适合烂在心里 提交于 2019-12-09 08:29:45

问题


I was wondering how do I find out how many bytes does a character have?


回答1:


If you want to find out how many UTF-8 bytes a letter in a PHP string has then:

print strlen(mb_substr($string, 0, 1, "utf-8"));

strlen() returns the raw byte length, while mb_substr() returns a "character" according to the charset/encoding. In this example from position 0.




回答2:


  • ASCII is 7 bits.
  • Most other languages use 8 bits (1 byte).
  • Many eastern languages (Chinese, Japanese) use 16 bits (2 bytes).
  • Unicode is usually 32 bits (4 bytes).

How a character is stored and represented depends on the programming language and the platform you are using.



来源:https://stackoverflow.com/questions/6086692/how-do-i-find-out-how-many-bytes-a-character-has

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!