On Sunday, 17 July 2016 21:42:26 UTC+3, EdgarAlejandro Vintimilla wrote: > > Hi, I have a GPS that sends me data through a connection TCP > > the data it sends me are in ASCII, and I have to convert it to HEX > > for example in python I'm doing this > > BUFFER_SIZE = 1024 > conn, addr = s.accept() > data = conn.recv(BUFFER_SIZE) > data.encode("hex") > conn.close() > print data > > and it works. > > but in GO, > > buf := make([]byte, 1024) > reqLen, err := conn.Read(buf) >
Note, from here you are not using reqLen, you probably should do: buf = buf[:reqLen], although if the conn is not fast enough you may not receive the full request. > if err != nil { > fmt.Println("Error: ", err.Error()) > } > > fmt.Println("buf: ", buf) > fmt.Println("buf str: ", string(buf)) > this []byte to string conversion assumes that "buf" is encoded as UTF8, so if you have bytes that are larger than 0x7f you might get bizarre results. *(Although I know you mentioned ASCII, it might also be Extended ASCII)* > var str string = "" > for i:=0; i< len(buf); i++{ > str += strconv.FormatUint(uint64(buf[i]), 16) > } > fmt.Println("str: ", str) > > or if I use io, error2 :=ioutil.ReadAll(connection) i do not get the exact > data that it sends me in any way > data, err := ioutil.ReadAll(conn) if err != nil { panic(err) } enc := hex.EncodeToString(data) fmt.Println("hex:", enc) Also you should be probably reading up to the message sequence not everything. Protocols usually define some ending character or sequence, or have a leading length of message. I suspect you should read up-to a line-feed instead of everything. And also that might be the reason that you get different results from Go and Python. + Egon -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.