资讯动态

string和byte的相互转换

发布时间:2026/10/15 8:53:11 来源:尧图企业网站定制
string和byte的相互转换package main import ( fmt ) func main() { ch1 : a ch2 : b fmt.Printf(ch1的类型是: %T, ch2的类型是: %T\n, ch1,ch2) fmt.Printf(ch1的数值是: %c, ch2的数值是: %c\n, ch1,ch2) // byte 转换为 字符串⬇️ str1 : string(ch1) str2 : string(ch2) fmt.Printf(str1的类型是: %T, str2的类型是: %T\n, str1,str2) fmt.Printf(str1的数值是: %s, str2的数值是: %s\n, str1,str2) // 字符串 转换为 byte⬇️ ch3 : str1[0] ch4 : str2[0] fmt.Printf(ch3的类型是: %T, ch4的类型是: %T\n, ch3,ch4) fmt.Printf(ch3的数值是: %c, ch4的数值是: %c\n, ch3,ch4) /* 注⚠️byte(str1) 不能把字符串转成单个 bytebyte 只能存一个字节而字符串可能包含多个字节 a 是字符字面量不是 byte。用 ch1 : a 声明时Go 将它推断为 runerune 是 int32 的别名所以 %T 输出 int32。 如果想让 ch1 是 byte可以写 ch1 : byte(a)。而 main.go:20 中的 str1[0] 取的是字符串的第 一个字节所以 ch3 的类型是 uint8byte 的别名。 */ }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑