FAQ
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.
http://play.golang.org/p/JX80hZfvc1

Is there a better way to do this?

Thanks,
Jay

--

Search Discussions

  • Daniel Jo at Dec 14, 2012 at 3:22 am
    Create the slice inline:

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

    -Daniel
    On Thu, Dec 13, 2012 at 8:16 PM, Jay Weisskopf wrote:

    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.
    http://play.golang.org/p/JX80hZfvc1

    Is there a better way to do this?

    Thanks,
    Jay

    --

    --
  • Jay Weisskopf at Dec 14, 2012 at 3:26 am
    Thanks. That's pretty interesting. So Go auto-magically creates a
    slice for you if you index an array that way?

    --
  • Jesse McNelis at Dec 14, 2012 at 3:30 am

    On Fri, Dec 14, 2012 at 2:26 PM, Jay Weisskopf 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.
    http://golang.org/doc/effective_go.html#slices

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

    --
  • Daniel Jo at Dec 14, 2012 at 4:06 am
    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
    On Thu, Dec 13, 2012 at 8:26 PM, Jay Weisskopf wrote:

    Thanks. That's pretty interesting. So Go auto-magically creates a
    slice for you if you index an array that way?
    --
  • Kevin Gillette at Dec 14, 2012 at 7:16 am

    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 at Dec 14, 2012 at 7:22 am
    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 at Dec 14, 2012 at 7:25 am
    http://play.golang.org/p/o3nKoXs3BL, maybe ?
    On Fri, Dec 14, 2012 at 6:22 PM, Jay Weisskopf wrote:
    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

    --
    --
  • Minux at Dec 14, 2012 at 7:31 am

    On Fri, Dec 14, 2012 at 3:22 PM, Jay Weisskopf wrote:

    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
    you can use bytes.IndexByte or strings.Index for this task.

    --
  • Kevin Gillette at Dec 15, 2012 at 12:12 am

    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.
    On Friday, December 14, 2012 12:22:22 AM UTC-7, Jay Weisskopf wrote:

    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
    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

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 14, '12 at 3:16a
activeDec 15, '12 at 12:12a
posts10
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase