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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题