kramer65
kramer65

Reputation: 53873

How to convert these (hex) numbers to characters in Golang?

I receive some characters over a radio chip which I try to read out from a serial port. I can read it out fine in this Python code, which gives me this:

received: counter: 2703
received: counter: 2704
received: counter: 2705

So using the go-serial package I wrote some code to do the same in Go:

package main

import "fmt"
import "log"
import "github.com/jacobsa/go-serial/serial"
import "io"
import "encoding/hex"


func main() {
        // Set up options.
        options := serial.OpenOptions{
                PortName: "/dev/ttyUSB0",
                BaudRate: 9600,
                DataBits: 7,
                StopBits: 2,
                MinimumReadSize: 4,
        }

        // Open the port.
        port, err := serial.Open(options)
        if err != nil {
                log.Fatalf("serial.Open: %v", err)
        }
        defer port.Close()

        for {
                buf := make([]byte, 32)
                n, err := port.Read(buf)
                if err != nil {
                        if err != io.EOF {
                                fmt.Println("Error reading from serial port: ", err)
                        }
                } else {
                        buf = buf[:n]
                        fmt.Println("received: ", buf)
                        fmt.Println("received: ", hex.EncodeToString(buf))
                }
        }
}

As you can see I print out the received buffer both raw AND converted from hex to string. The result is this:

received:  [99 111 117 110 116 101 114 58 32 51 48 50 52 10]
received:  636f756e7465723a20333032340a
received:  [99 111 117 110 116 101 114 58 32 51 48 50 53 10]
received:  636f756e7465723a20333032350a
received:  [99 111 117 110 116 101 114 58 32 51 48 50 54 10]
received:  636f756e7465723a20333032360a

I guess those numbers represent counter: 2704, but as you can see the conversion to string doesn't give me the result I expect.

What am I doing wrong here? How can I convert those numbers to a string?

Upvotes: 0

Views: 5044

Answers (2)

kramer65
kramer65

Reputation: 53873

I found that the solution is very simple. Instead of either of these:

fmt.Println("received: ", buf)
fmt.Println("received: ", hex.EncodeToString(buf))

I simply had to do this:

fmt.Println("received: ", string(buf))

Upvotes: 1

torek
torek

Reputation: 488363

The text that came in is already a valid string. It's just that you have the bytes stored in buf which is []byte. To convert the existing []byte to a value of type string:

asString := string(buf)

While hex.EncodeToString returns a string, it returns the hexadecimal representation of each byte. For instance, the UTF-8 / ASCII for lowercase c, code 99 decimal, is 0x63, so the first two characters of hex.EncodeToString are 6 and 3.

(Meanwhile, you should figure out what to do with actual errors. Your code currently ignores them, after announcing any that are not io.EOF. If your device goes into an error state, you will loop over and over again getting the same error.)

Upvotes: 3

Related Questions