如何确定 UTF-8 编码字符串的实际字节长度?

更新于
2026-09-29 11:49:16
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计154个文字,预计阅读时间需要1分钟。

如何确定 UTF-8 编码字符串的实际字节长度?

pythonCount bytes in a string's UTF-8 representation.

https://codereview.stackexchange.com/a/37552

@param string

@return int

/function getByteLen(normal_val)

{ // Force string type normal_val=String(normal_val); var b=0; for (var i=0; i 0x7F && code 0x7FF && code =0x10000 && code <=0x10FFFF) b +=3; } return b;}

gistfile1.txt

/** * Count bytes in a string's UTF-8 representation. * codereview.stackexchange.com/a/37552 * @param string * @return int */ function getByteLen(normal_val) { // Force string type normal_val = String(normal_val); var byteLen = 0; for (var i = 0; i < normal_val.length; i++) { var c = normal_val.charCodeAt(i); byteLen += c < (1 << 7) ? 1 : c < (1 << 11) ? 2 : c < (1 << 16) ? 3 : c < (1 << 21) ? 4 : c < (1 << 26) ? 5 : c < (1 << 31) ? 6 : Number.NaN; } return byteLen; }

如何确定 UTF-8 编码字符串的实际字节长度?

本文共计154个文字,预计阅读时间需要1分钟。

如何确定 UTF-8 编码字符串的实际字节长度?

pythonCount bytes in a string's UTF-8 representation.

https://codereview.stackexchange.com/a/37552

@param string

@return int

/function getByteLen(normal_val)

{ // Force string type normal_val=String(normal_val); var b=0; for (var i=0; i 0x7F && code 0x7FF && code =0x10000 && code <=0x10FFFF) b +=3; } return b;}

gistfile1.txt

/** * Count bytes in a string's UTF-8 representation. * codereview.stackexchange.com/a/37552 * @param string * @return int */ function getByteLen(normal_val) { // Force string type normal_val = String(normal_val); var byteLen = 0; for (var i = 0; i < normal_val.length; i++) { var c = normal_val.charCodeAt(i); byteLen += c < (1 << 7) ? 1 : c < (1 << 11) ? 2 : c < (1 << 16) ? 3 : c < (1 << 21) ? 4 : c < (1 << 26) ? 5 : c < (1 << 31) ? 6 : Number.NaN; } return byteLen; }

如何确定 UTF-8 编码字符串的实际字节长度?