newbie: string to byte array

Showing 1-5 of 6 messages
newbie: string to byte array Thomas Kaufmann 12/9/10 4:57 AM
Hi,

how can I convert a string in a byte array?

o-o

Thomas
Re: [go-nuts] newbie: string to byte array Rob 'Commander' Pike 12/9/10 4:59 AM
On Thu, Dec 9, 2010 at 7:57 AM, Thomas Kaufmann <tok...@googlemail.com> wrote:
> Hi,
>
> how can I convert a string in a byte array?

By converting a string [to] a byte array:

    []byte("string")

-rob

Re: [go-nuts] newbie: string to byte array Andrew Gerrand 12/9/10 1:57 PM
To expand a little: this is how type conversions work more generally in Go.

To convert variable v to type T, you would do:
   T(v)

So, to do the reverse of Rob's example, you can convert the []byte b
to a string like so:
   string(b)

Andrew

Re: newbie: string to byte array Jim Teeuwen 12/9/10 3:20 PM
Incidentally, this also works for fetching unicode runes from a string
and in reverse:

runes := []int("foo")
str := string(runes)
Re: [go-nuts] newbie: string to byte array fixe...@gmail.com 6/7/15 9:30 AM
Quick point of clarificaton (for my own understanding): This converts the string to a byte slice, right? Converting to a byte array would imply that the length of the array were statically fixed at compile time, and there's no way to statically guarantee the string has a particular length.

(This is still almost certainly the right thing to do; I just want to confirm I'm not misunderstanding the typing rules in this context).