Best way to convert byte array (not slice) to string?

14,414 views
Skip to first unread message

Jay Weisskopf

unread,
Dec 13, 2012, 10:16:16 PM12/13/12
to golan...@googlegroups.com
I am reading a (legacy) binary file format that contains strings of a fixed size. I am currently reading the string data into a byte array (via encoding/binary). After that, I'd like to use the string type instead. However, it doesn't seem like there is any minimalist way to convert from a byte array to a string.

The current strategy I'm using is to create a temporary byte slice, iterate over each element and copy from the array to the slice, then create a string from the byte slice.

Is there a better way to do this?

Thanks,
Jay

Daniel Jo

unread,
Dec 13, 2012, 10:22:11 PM12/13/12
to Jay Weisskopf, golan...@googlegroups.com
Create the slice inline:

src := [10]byte{'h', 'e', 'l', 'l', 'o'}
str := string(src[:]) // slice the array from beginning to end

-Daniel

--
 
 

Jay Weisskopf

unread,
Dec 13, 2012, 10:26:04 PM12/13/12
to Daniel Jo, golan...@googlegroups.com
Thanks. That's pretty interesting. So Go auto-magically creates a
slice for you if you index an array that way?

Jesse McNelis

unread,
Dec 13, 2012, 10:30:42 PM12/13/12
to Jay Weisskopf, Daniel Jo, golang-nuts
On Fri, Dec 14, 2012 at 2:26 PM, Jay Weisskopf <jays...@gmail.com> wrote:
Thanks. That's pretty interesting. So Go auto-magically creates a
slice for you if you index an array that way?

When you slice an array you get a slice of that array, if you slice a slice you get a slice of that slice.

You can slice strings too and you'll get a string that is a slice of the original string.

Daniel Jo

unread,
Dec 13, 2012, 11:06:07 PM12/13/12
to Jay Weisskopf, golan...@googlegroups.com
The only "auto-magical" part would be the automatic determination of the source array's (or source slice's) bounds. The manual way of doing the same thing would be:

slc := src[0:len(src)]

Otherwise, it's just Go's basic slicing operation. It's pretty much the same syntax that Python uses for slicing lists (sans negative indices).

-Daniel

Kevin Gillette

unread,
Dec 14, 2012, 2:16:45 AM12/14/12
to golan...@googlegroups.com, Jay Weisskopf
On Thursday, December 13, 2012 9:06:07 PM UTC-7, Ostsol wrote:
Otherwise, it's just Go's basic slicing operation. It's pretty much the same syntax that Python uses for slicing lists (sans negative indices).

Though with the important difference that Go slices can't take Python's extended slice form(s), and cannot take a 'step', and while Python always semantically copies when a slice occurs, Go never does.

Jay Weisskopf

unread,
Dec 14, 2012, 2:22:22 AM12/14/12
to Kevin Gillette, golan...@googlegroups.com
I'm now having an issue with len(). It still returns the size of the
underlying array, not what I would consider the "correct" length of
the string (number of chars until null):
http://play.golang.org/p/x3Xz-4yRkZ

Dave Cheney

unread,
Dec 14, 2012, 2:25:00 AM12/14/12
to Jay Weisskopf, Kevin Gillette, golan...@googlegroups.com

minux

unread,
Dec 14, 2012, 2:26:32 AM12/14/12
to Jay Weisskopf, Kevin Gillette, golan...@googlegroups.com
you can use bytes.IndexByte or strings.Index for this task.

Kevin Gillette

unread,
Dec 14, 2012, 7:05:14 PM12/14/12
to golan...@googlegroups.com, Kevin Gillette
On Thursday, December 13, 2012 8:26:04 PM UTC-7, Jay Weisskopf wrote:
Thanks. That's pretty interesting. So Go auto-magically creates a 
slice for you if you index an array that way? 

It's not too much of a magic trick, considering that slices are just a struct roughly like:

struct {
  data unsafe.Pointer,
  len, cap int
}

Due to this, an "automagic slice" of a 20mb string is exactly as cheap as an automagic slice of a 20 byte string, since no copies are involved.
Go is not C.  A "string" in Go is like a slice header (that struct above), except it does not have a cap, and very specifically, a Go string consisting of 20 null bytes still has a "string length" of 20. In Go, there is no '\0': it's '\x00', since '\0' is not all that useful here.  All this means that no matter the length of your string, you always have length information immediately available for the low cost of the size of an int (4-8 bytes), as opposed to regular C strings, which have linear-time length determinations and are susceptible to a large number of problems.

That said, len is doing precisely what it's defined to do. To graft C-like behavior on top of Go strings, you may do: http://play.golang.org/p/g46t7xtb_1

juanalber...@gmail.com

unread,
Jul 19, 2018, 2:47:58 PM7/19/18
to golang-nuts
Excellent!, Thank you
Reply all
Reply to author
Forward
0 new messages