Go string to ascii byte array

GoByteAscii

Go Problem Overview


How can I encode my string as ASCII byte array?

Go Solutions


Solution 1 - Go

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

The language spec details conversions between strings and certain types of arrays (byte for bytes, int for Unicode points)

Solution 2 - Go

You may not need to do anything. If you only need to read bytes of a string, you can do that directly:

c := s[3]

cthom06's answer gives you a byte slice you can manipulate:

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

Then you can create a new string from the modified byte slice if you like:

s = string(b)

But you mentioned ASCII. If your string is ASCII to begin with, then you are done. If it contains something else, you have more to deal with and might want to post another question with more details about your data.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser181351View Question on Stackoverflow
Solution 1 - Gocthom06View Answer on Stackoverflow
Solution 2 - GoSoniaView Answer on Stackoverflow