What's the best way to convert from []uint8 to string?
I'm using http://github.com/confluentinc/confluent-kafka-go/kafka
To read events from kafka. But it does not return plain string event.
It returns event with type []uint8.
How can I convert this event from []uint8 to string?
byte is an alias for uint8, which means that a slice of uint8) (aka []uint8) is also a slice of byte (aka []byte).
And byte slices and strings are directly convertible, due to the fact that strings are backed by byte slices:
myByteSlice := []byte{ ... } // same as myByteSlice := []uint8{ ... }
myString := string(myByteSlice) // myString is a string representation of the byte slice
myOtherSlice := []byte(myString) // Converted back to byte slice
Related
func main(){
bytearray:=[]byte{"data"}
reader := bytes.NewReader(stdout.Bytes())
transfer(reader)
}
Function 2
func transfer(reader *Reader){
bytearray:= //How do I get the original byte array?
}
Basically I want to send byte array from one function to another using readers or writers
bytes.Buffer is what you need. It can convert a byte slice to an io.Reader/io.Writer:
buf := bytes.NewBuffer([]bytes{...})
And to read from an io.Reader into a byte slice:
s, err := ioutil.ReadAll(r)
Converting to/from byte arrays is left as a trivial exercise for the reader.
how can I convert type []string to []rune?
I know you can do it like this:
[]rune(strings.Join(array,""))
but is there a better way?
I would prefer not to use strings.Join(array,"") for this purpose because it builds one big new string I don't need. Making a big string I don't need is not space-efficient, and depending on input and hardware it may not be time-efficient.
So instead I would iterate through the array of string values and convert each string to a rune slice, and use the built-in variadic append function to grow my slice of all rune values:
var allRunes []rune
for _, str := range array {
allRunes = append(allRunes, []rune(str)...)
}
I've got a Go TCP server which accepts connections, I'd like to echo the messages back 1 byte at a time, I can't see a way to get net.Conn to send a single byte using net.Conn.Write
c.Write([]byte(b))
cannot convert b (type byte) to type []byte
c.Write(b)
cannot use b (type byte) as type []byte in argument to c.Write
an io.Writer always accepts a []byte as the argument. Use a 1 byte long byte slice. What you tried ([]byte(b)) was to convert a single byte to a byte slice. Instead, create a one-element byte slice with b as the only element:
n, err := c.Write([]byte{b})
Use b[i:i+1] to create a one byte slice:
s := "Hello world!"
b := []byte(s)
for i:=0; i<len(s); i++ {
c.Write(b[i:i+1])
}
I am trying to take a string and convert each value in the string into the decimal ASCII value. I first converted the string into the []byte type and i want to take each element of the []byte and convert it into decimal ASCII value. Here is my code:
myArray := []byte(password) // convert string into []byte type
NumArray := [len(password)]int // create second []int type to store the converted []byte elements
for i := 0; i < len(myArray); i++{
/* I need some help to convert each element in myArray into ASCII decimal value and then store it into
NumArray.
*/
fmt.Printf("%d\n", myArray[i]) //prints out what the converted values should be
fmt.Print(NumArray[i]) //prints out the stored converted value for comparison
}
Edit: the string is supposed to be a password and so can contain any value
You can cast byte to int like this:
NumArray[i] = int(myArray[i])
I have this code:
func my_function(hash string) [16]byte {
b, _ := hex.DecodeString(hash)
return b // Compile error: fails since [16]byte != []byte
}
b will be of type []byte. I know that hash is of length 32. How can I make my code above work? Ie. can I somehow cast from a general-length byte array to a fixed-length byte array? I am not interested in allocating 16 new bytes and copying the data over.
There is no direct method to convert a slice to an array. You can however do a copy.
var ret [16]byte
copy(ret[:], b)
The standard library uses []byte and if you insist on using something else you will just have a lot more typing to do. I wrote a program using arrays for my md5 values and regretted it.