python与go字符串编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wshk918/article/details/85535752

python中字符串(str)是Unicode编码的,所以中文和英文字母都占两个字节(一个字节的话,最大为256)。英文字母数值与Ascii保持一致。

a="啦"
print("Unicode编码为:",ord(a))
print("英文字母%s编码为:%d"%("b",ord("b")))
b=a.encode(encoding="utf-8")
print(type(b))
print(list(b))


输出:
Unicode编码为: 21862
英文字母b编码为:98
<class 'bytes'>
[229, 149, 166]

go语言字符串则是utf-8的编码,英文占一个字节,其他的占1-4个。中文占3个。

package main

import "fmt"

func main() {
	a := "啦b"
	fmt.Printf("字符串的长度为%d\n", len(a))
	for i := 0; i < len(a); i++ {
		fmt.Println(a[i])
	}
}

输出为:
字符串的长度为4
229
149
166
98

猜你喜欢

转载自blog.csdn.net/wshk918/article/details/85535752