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 int
s 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.