Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign string to bytes array

Tags:

go

I want to assign string to bytes array:

var arr [20]byte str := "abc" for k, v := range []byte(str) {   arr[k] = byte(v) } 

Have another method?

like image 854
sofire Avatar asked Nov 07 '11 02:11

sofire


People also ask

How do you assign a byte array?

Arrays. fill(). This method assigns the required byte value to the byte array in Java. The two parameters required for java.

Can we convert string to byte array in java?

A String is stored as an array of Unicode characters in Java. To convert it to a byte array, we translate the sequence of characters into a sequence of bytes. For this translation, we use an instance of Charset. This class specifies a mapping between a sequence of chars and a sequence of bytes.

Is byte array same as string?

Since bytes is the binary data while String is character data. It is important to know the original encoding of the text from which the byte array has created. When we use a different character encoding, we do not get the original string back.


1 Answers

Safe and simple:

[]byte("Here is a string....") 
like image 114
openwonk Avatar answered Sep 28 '22 12:09

openwonk