English 中文(简体)
重新分类的[]细化工序
原标题:Null-terminated []byte to string doesn t work
  • 时间:2024-03-23 02:49:38
  •  标签:
  • go

I have a slice of bytes to represent a null-terminated string with trailing garbage bytes.

页: 1 我怎么能够将一个零度的沥青阵列转化为扼杀?

s := string(byteArray[:n])

但它没有工作

[54 49 0 101 0 0 0 0 64 31 1 0 0]
61e@

页: 1

https://go.dev/play/p/FDPPLgGjHOY”rel=“nofollow noreferer”>https://go.dev/play/p/FDPPLgGjHOY

问题回答

你们首先必须找到:

ix:=bytes.IndexByte(byteArray,0)
if ix==-1 {
  ix=len(byteArray)
}
s:=string(byteArray[:ix])




相关问题
minimum work size of a goroutine [closed]

Does anyone know approximately what the minimum work size is needed in order for a goroutine to be beneficial (assuming that there are free cores for the work to be offloaded to)?

How do you get the terminal size in Go?

How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go

What do you use to write Go [closed]

I know its a bit too early, but I ve been trying out Go (Google s Programming Language) and its kindof annoying to write code in gedit. So, my question: What do you use to experiment with Go?

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

热门标签