【问题标题】:Can't remove Null terminator from byte array?无法从字节数组中删除 Null 终止符?
【发布时间】:2018-06-04 04:17:30
【问题描述】:

我在 Go(在 OSX 上)中使用 this library 与 Windows DNS 服务器交互。

运行下面的 sn-p 时,我收到一个关于空终止符的错误。

 $ ~/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

我怀疑当辅助方法调用 sprintf 将 json 响应连接到数组中时,添加了一个空终止符 here

但是,即使添加了如下所示的bytes.Trim... 我仍然收到空终止符错误,而且空终止符似乎仍然存在...

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)

打电话时我得到以下信息

(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

【问题讨论】:

    标签: go


    【解决方案1】:

    从您的日志中,违规字符似乎位于 2102 位置,而整个数组具有 2615 元素。

    所以看起来Trim 不会解决它,因为问题不一定是数组的最后一个字符。

    您是否尝试删除所有匹配项,例如使用 Replace

    byteRespTrim = bytes.Replace(byteRespTrim, []byte("\x00"), []byte{}, -1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多