duanna3634 2018-06-03 19:52
浏览 61
已采纳

无法从字节数组中删除Null终止符?

I'm using this library in Go (on OSX) to interact with a windows DNS server.

When running the below snippet, I get an error about a null terminator.

 $ ~/go/bin/winrm-dns-client create -d domain.com -n node-0 -t A -v 10.90.61.30
2018/06/03 12:40:22 Error creating DNS record: Reading record: Reading record: Unmarshalling response: Unmarshalling json: invalid character '\x00' after array element

My suspicion is that there's a null terminator added here when the helper method calls sprintf to concat json responses into an array.

However, even after adding a bytes.Trim like shown below... I still get a null terminator error and it seems that the null terminator still exists...

func unmarshalResponse(resp string) ([]interface{}, error) {
    var data interface{}
    byteRespTrim := []byte(resp)
    fmt.Print("found a null terminator at -- ")
    fmt.Println(bytes.Index(byteRespTrim, []byte("\x00")))
    fmt.Print("total length = ")
    fmt.Println(len(byteRespTrim))
    byteRespTrim = bytes.Trim(byteRespTrim, "\x00")
    fmt.Print("after trim found a null terminator at -- ")
    loc := bytes.Index(byteRespTrim, []byte("\x00"))
    fmt.Print(loc)

When calling I get the below

(master)⚡ % ./windows-dns-test create -d domain.com -n openshift-node-0 -t A -v 10.90.61.30                       
found a null terminator at -- 2102
total length = 2615
after trim found a null terminator at -- 2102
  • 写回答

1条回答 默认 最新

  • duanmian1085 2018-06-03 20:17
    关注

    From your logs, the offending character seems to be found at position 2102, while the whole array has 2615 elements.

    So it looks Trim won't solve it since the problem is not necessarily the final character of the array.

    Did you try removing all occurrences, using Replace for example?

    byteRespTrim = bytes.Replace(byteRespTrim, []byte("\x00"), []byte{}, -1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果