如何将字节缓冲区中的以空字符结尾的字符串转换为Go中的字符串? [英] How can I convert a null-terminated string in a byte buffer to a string in Go?
本文介绍了如何将字节缓冲区中的以空字符结尾的字符串转换为Go中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
label:= string([] byte {97,98,99,0,0,0, 0})
fmt.Printf(%s \ n,label)
( ^ @
是空字节):运行test.go
abc ^ @ ^ @ ^ @
解决方案
使用字符串
包。
package main
导入(
fmt
字符串
)
func main(){
label:= string([ ]字节{97,98,99,0,0,0,0})
fmt.Println(strings.TrimSpace(label))
}
This:
label := string([]byte{97, 98, 99, 0, 0, 0, 0})
fmt.Printf("%s\n", label)
does this (^@
is the null-byte):
go run test.go
abc^@^@^@
解决方案
use the strings
package.
package main
import (
"fmt"
"strings"
)
func main() {
label := string([]byte{97, 98, 99, 0, 0, 0, 0})
fmt.Println(strings.TrimSpace(label))
}
这篇关于如何将字节缓冲区中的以空字符结尾的字符串转换为Go中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文