Is there any way to convert []byte to int or I can send int to the server?
A byte in GO is alias for uint8 so there will be a mismatch in sign when you try to convert it to int, since:
uint8 ranges: 0 through 255 and (unsigned and same as byte) However,
int8 ranges: –128 through 127 (signed)
So as a result you would only be able to convert half of the range, and lose the other half which would return 0.
Doing this could understandably result in a loss of data, bugs and it's generally not a good idea.
The best way would be to code your application in a way that respect the datatypes.
package main
import (
"fmt"
)
func main() {
byteslice := []byte{159,127,208,150,211,126,210,192,227,247,240,207,201,36,190,239,79,252,235,104}
// convert byte to uint8 (it's actually the same)
fmt.Printf("0x%02X type: %T \n", byteslice[0], byteslice[0])
byte_0 := byteslice[0]
fmt.Printf("0x%02X type: %T \n\n", byte_0, byte_0)
// convert to various bases
base2 := ""
base10 := ""
base16 := ""
for _, b := range byteslice {
base2 += fmt.Sprintf("%08b", b)
base10 += fmt.Sprintf("%d", b)
base16 += fmt.Sprintf("%02X", b)
}
fmt.Printf("base2 %s len %d bits \nbase10 %s \nbase16 %s len %d nibbles \\2 = %d bytes\n",base2, len(base2), base10, base16, len(base16), len(base16)/2)
}
Go Playground