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

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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格