专业的编程技术博客社区

网站首页 > 博客文章 正文

「golang」 string和「」byte 相互转换

baijin 2024-08-15 17:09:04 博客文章 5 ℃ 0 评论

golang 中的string 和 []byte 相互转换

string to [] byte

	var strhello string = "hello world"
	var bytehello []byte = []byte(strhello)
	fmt.Printf("%s [%x]\n", bytehello, bytehello)

输出:

hello world [68656c6c6f20776f726c64]

[]byte to string

	var bytehello []byte = []byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
	var stringhello string = string(bytehello[:])
	fmt.Printf("%s [%x]\n", stringhello, stringhello)

输出:

hello world [68656c6c6f20776f726c64]

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表