Go to string in ascii byte array

How can I encode my string as an array of ASCII bytes?

+61
go
Jul 30 '10 at 13:10
source share
2 answers

If you are looking for a conversion, just byteArray := []byte(myString)

The language specification details conversions between strings and some types of arrays (bytes for bytes, int for Unicode points)

+108
Jul 30 '10 at 13:18
source share

You do not have to do anything. If you only need to read the bytes of the string, you can do this directly:

 c := s[3] 

Cthom06's answer gives you a byte fragment that you can manipulate:

 b := []byte(s) b[3] = c 

Then you can create a new line from the modified byte fragment if you want:

 s = string(b) 

But you mentioned ASCII. If you start with an ASCII string, then you're done. If there is anything else in it, you need to deal more and may want to post another question with more details about your data.

+8
Oct 19 '12 at 13:12
source share



All Articles