doulao1934 2017-10-15 13:23
浏览 60
已采纳

在网址参数中获取字节数组作为字符串

Basically, here's my problem: I have a device connected to SigFox which sends 12 bytes of data, which is then rerouted to my golang server using a callback. I tried going the usual route, and decoding the information sent by the callback as a json with json.NewDecoder(r.Body).Decode(&m), where m is a struct that contains the information sent by the SigFox api on the callback, aka:

type SigFoxMessage struct {
    Time int64 `json:"time"` // I'm getting an epoch timestamp, which I then convert to time.Time
    Data [12]byte `json:"data"`
    Lat int `json:"lat"`
    Lng int `json:"lng"`
}

However, this was always giving me an EOF error, and so I decided to go with r.ParseForm(), and r.Form["key"]. The only problem with this new approach is that it returns the values of the parameters as strings, which isn't a big deal when dealing with the ints and the int64, as I can just use the strconv package. The problem is the 12 bytes of data.

Since they are received as a string, that means I get something like "3132333435363738393130" (the 12 bytes in hex, so 2 characters per byte is a certainty), which I want to convert to [12]byte{31, 32, 33, 34, 35, 36, 37, 38, 39, 31, 30}. But I can't find anyway to do this besides actually implementing a function to parse it. I tried searching for string to byte array conversion, but all I can find are ways to turn byte arrays into strings (which would be turing my [12]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0'} into "12345678910", and which doesn't interest me), or how to turn strings into array of characters ("3132333435363738393130" -> []byte{'3', '1', '3', ... }), which also doesn't interest me.

Anyways, is there an actual way to do it without having to write the code to parse it?

Also, any ideas why the json decoder might not have worked?

Edit: I should have put this right away, this is what the params of the request look like:

?time=1507834946&data=3132333435363738393130&lat=-8.0&lng=-35.0

Edit 2: The data parameter has the bytes in hex, so every two characters correspond to one byte, ranging from 00 to ff. The example case I provided was a counting error when trying to send the message with SigFox, and has only 11 bytes. From what I've seen of Go, that shouldn't be a problem, as I believe it would then complete the missing byte, so I don't think that was the reason json decoder failed, but I'll try it with the correct number of bytes now.

  • 写回答

1条回答 默认 最新

  • dpd46554 2017-10-15 15:30
    关注

    Use the encoding/hex package.

    r.ParseForm()
    var message SigFoxMessage
    n, err := hex.Decode(message.Data[:], []byte(r.FormValue("data")))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("decoded %d bytes: %q
    ", n, message.Data[:])
    

    https://play.golang.org/p/kfW1t2ADsR

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变
  • ¥100 如何帮我写一个java小游戏