In GO I get this data buf := make([]byte, 1024) buf: [64 64 153 0 4 50 49 51 71 76 50 48 49 52 48 49 53 49 56 55 0 0 0 0 0 16 1 233 24 140 87 64 25 140 87 68 88 10 0 136 2 0 0 182 29 0 0 4 0 0 0 4 0 7 100 1 17 19 0 3 0 1 17 7 16 23 43 59 76 94 160 0 244 82 243 16 7 0 0 0 0 73 68 68 95 50 49 51 71 48 50 95 83 32 86 50 46 50 46 54 0 73 68 68 95 50 49 51 71 48 50 95 72 32 86 50 46 50 46 54 0 13 0 1 24 2 24 1 26 1 30 1 31 2 31 3 31 4 31 5 31 6 31 7 31 1 33 2 33 224 55 13 10] []uint8
cadena += fmt.Sprintf("%x ", buf[i]) cadena: 40 40 99 0 4 32 31 33 47 4c 32 30 31 34 30 31 35 31 38 37 0 0 0 0 0 10 1 e9 18 8c 57 40 19 8c 57 44 58 a 0 88 2 0 0 b6 1d 0 0 4 0 0 0 4 0 7 64 1 11 13 0 3 0 1 11 7 10 17 2b 3b 4c 5e a0 0 f4 52 f3 10 7 0 0 0 0 49 44 44 5f 32 31 33 47 30 32 5f 53 20 56 32 2e 32 2e 36 0 49 44 44 5f 32 31 33 47 30 32 5f 48 20 56 32 2e 32 2e 36 0 d 0 1 18 2 18 1 1a 1 1e 1 1f 2 1f 3 1f 4 1f 5 1f 6 1f 7 1f 1 21 2 21 e0 37 d string and python 4040990004323133474c323031343031353138370000000000100111a38d5766a38d57855d0a0000000000c21d0000010000000400076401111e000300011307100331317e4aa000f245f310000000008c4944445f3231334730325f532056322e322e36004944445f3231334730325f482056322e322e36000d0001180218011a011e011f021f031f041f051f061f071f0121022171a40d0a ------------------------------------------------------------------------------------------------------------------- On Monday, July 18, 2016 at 1:23:05 AM UTC-5, Egon wrote: > > What is the difference in the data that you receive? > > On Monday, 18 July 2016 05:42:36 UTC+3, EdgarAlejandro Vintimilla wrote: >> >> now I have this, but still not the correct results >> >> package main >> >> import ( >> "fmt" >> "net" >> "os" >> //"strconv" >> //"bytes" >> //"io/ioutil" >> //"net/http" >> "reflect" >> //"strings" >> ) >> >> const ( >> CONN_HOST = "" >> CONN_PORT = "5555" >> CONN_TYPE = "tcp" >> ) >> >> func main() { >> // Listen for incoming connections. >> l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) >> if err != nil { >> fmt.Println("Error listening:", err.Error()) >> os.Exit(1) >> } >> // Close the listener when the application closes. >> defer l.Close() >> fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT) >> for { >> // Listen for an incoming connection. >> conn, err := l.Accept() >> if err != nil { >> fmt.Println("Error accepting: ", err.Error()) >> os.Exit(1) >> } >> // Handle connections in a new goroutine. >> go handleRequest(conn) >> } >> } >> >> // Handles incoming requests. >> func handleRequest(conn net.Conn) { >> >> // Make a buffer to hold incoming data. >> buf := make([]byte, 1024) >> >> // Read the incoming connection into the buffer. >> reqLen, err := conn.Read(buf) >> buf = buf[:reqLen] >> >> if err != nil { >> fmt.Println("Error reading:", err.Error()) >> } >> >> var cadena string = "" >> for i := 0; i < len(buf)-1; i++ { >> cadena += fmt.Sprintf("%x ", buf[i]) >> } >> >> >> // Send a response back to person contacting us. >> conn.Write([]byte( "ok" )) >> fmt.Println("cadena: ", cadena, reflect.TypeOf(cadena) ) >> fmt.Println( "\n" ) >> >> // Close the connection when you're done with it. >> conn.Close() >> >> } >> >> >> >> On Sunday, July 17, 2016 at 2:05:22 PM UTC-5, Egon wrote: >>> >>> 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.