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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题