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

图片转代码服务由CSDN问答提供 功能建议

我正在使用此库(在OSX上)与Windows DNS服务器进行交互(在OSX上)。

在运行以下代码段时,我收到一条有关

  $〜/ go / bin / winrm-dns-client create -d domain.com -n node-0 -t A -v 10.90.61.30 \  n2018 / 06/03 12:40:22创建DNS记录时出错:读取记录:读取记录:编组响应:编组json:数组元素后的无效字符'\ x00'
   
 
  

我怀疑是添加了一个空终止符此处,当辅助方法调用 sprintf 将json响应合并到数组中时。

但是,即使添加了 bytes后也是如此。 修剪,如下所示...我仍然收到null终止符错误,而且似乎是null术语 指示符仍然存在...

  func unmarshalResponse(resp字符串)([] interface {},错误){
 var数据接口{} 
 byteRespTrim:= []  byte(resp)
 fmt.Print(“在-”)处找到一个空终止符
 fmt.Println(bytes.Index(byteRespTrim,[] byte(“ \ x00”)))
 fmt.Print(“ 总长度=“)
 fmt.Println(len(byteRespTrim))
 byteRespTrim = bytes.Trim(byteRespTrim,” \ x00“)
 fmt.Print(”修剪后在-处找到空终止符)\  n 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 修剪后在-2102 
总长度= 2615 
处找到一个空终止符在修剪后在-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)
    
    打赏 评论

相关推荐 更多相似问题