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

无法从字节数组中删除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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?