FAQ

On Tue, Sep 11, 2012 at 1:10 PM, Jake Brukhman wrote:
Context:

I have a struct that will be filled in from binary data. I know field "str"
is going to be a 4-byte string when I read it in.

type Data struct {
one int32
two int32
str [4]byte
}
data := new(Data)
and will want to interpret "str" as a string -- but what's the best way to
do the conversion and/or reading in in this case?
s := string(data.str[:])
You need to slice the array before converting it to a string.


--
=====================
http://jessta.id.au

Search Discussions

  • Jake Brukhman at Sep 11, 2012 at 3:39 am
    Context:

    I have a struct that will be filled in from binary data. I know field "str"
    is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way to
    do the conversion and/or reading in in this case?

    Thanks,
    Jake
  • Jorelli at Sep 11, 2012 at 4:01 am

    I'll do
    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    it's assumed what you mean is:

    var data Data
    binary.Read(someBuf, binary.BigEndian, &data)

    otherwise you just passed **Data into binary.Read instead of *Data

    could you perhaps plop in some sample input with the expected output? I'd
    like to help, but I don't know how to simulate the input offhand.

    something like in the example:

    b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}

    would be killer.

    otherwise if it's just six bytes:

    var data [6]byte
    binary.Read(someBuf, binary.BigEndian, &data)

    one := int32(data[0])
    two := int32(data[1])
    str := string(data[2:])

    I'm not sure if that actually works, I'm just curious to learn about binary
    handling.

    On Monday, September 10, 2012 11:10:30 PM UTC-4, Jake Brukhman wrote:

    Context:

    I have a struct that will be filled in from binary data. I know field
    "str" is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way to
    do the conversion and/or reading in in this case?

    Thanks,
    Jake


  • Jake Brukhman at Sep 11, 2012 at 1:07 pm
    Hi Jordan!

    Yes, you are right, I should have just passed in "data", not "&data".

    You can simulate the expected input with a buffer or file (e.g. io.Reader)
    that contains two int32's and a four character ASCII string:

    int32(100), int32(101), "HOLA"

    The output would be a Data struct populated with the analogous bytes of the
    above.

    Jake



    On Sep 11, 2012, at 12:01 AM, jorelli wrote:

    I'll do
    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    it's assumed what you mean is:

    var data Data
    binary.Read(someBuf, binary.BigEndian, &data)

    otherwise you just passed **Data into binary.Read instead of *Data

    could you perhaps plop in some sample input with the expected output? I'd
    like to help, but I don't know how to simulate the input offhand.

    something like in the example:

    b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}

    would be killer.

    otherwise if it's just six bytes:

    var data [6]byte
    binary.Read(someBuf, binary.BigEndian, &data)

    one := int32(data[0])
    two := int32(data[1])
    str := string(data[2:])

    I'm not sure if that actually works, I'm just curious to learn about binary
    handling.

    On Monday, September 10, 2012 11:10:30 PM UTC-4, Jake Brukhman wrote:

    Context:

    I have a struct that will be filled in from binary data. I know field
    "str" is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way to
    do the conversion and/or reading in in this case?

    Thanks,
    Jake


  • Jorelli at Sep 11, 2012 at 3:05 pm
    ah, ok. Well, a [4]string is an array of four strings, not an array
    describing a string of four characters. That would be [4]rune, but almost
    nothing takes array inputs because an array's size is a part of its type
    ([4]rune and [5]rune are distinct types); you're going to be left working
    with slices length checks to ensure consistency since [4]rune will be an
    almost-certainly meaningless input to any function.

    Passing a rune slice into binary.Read and then converting it to a string
    works.

    Anywho, I found a very ugly solution that my gut tells me must be subtly
    wrong in some way, specifically when handling strings of the wrong size or
    encoding:

    http://play.golang.org/p/bzGRmm7cbf

    It's assumed that you are not the ultimate source of the data. If you are,
    you can save yourself some headache by using the gob package.


    On Tuesday, September 11, 2012 9:07:41 AM UTC-4, Jake Brukhman wrote:

    Hi Jordan!

    Yes, you are right, I should have just passed in "data", not "&data".

    You can simulate the expected input with a buffer or file (e.g. io.Reader)
    that contains two int32's and a four character ASCII string:

    int32(100), int32(101), "HOLA"

    The output would be a Data struct populated with the analogous bytes of
    the above.

    Jake



    On Sep 11, 2012, at 12:01 AM, jorelli <jordan...@gmail.com <javascript:>>
    wrote:

    I'll do
    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    it's assumed what you mean is:

    var data Data
    binary.Read(someBuf, binary.BigEndian, &data)

    otherwise you just passed **Data into binary.Read instead of *Data

    could you perhaps plop in some sample input with the expected output? I'd
    like to help, but I don't know how to simulate the input offhand.

    something like in the example:

    b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}

    would be killer.

    otherwise if it's just six bytes:

    var data [6]byte
    binary.Read(someBuf, binary.BigEndian, &data)

    one := int32(data[0])
    two := int32(data[1])
    str := string(data[2:])

    I'm not sure if that actually works, I'm just curious to learn about
    binary handling.

    On Monday, September 10, 2012 11:10:30 PM UTC-4, Jake Brukhman wrote:

    Context:

    I have a struct that will be filled in from binary data. I know field
    "str" is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way
    to do the conversion and/or reading in in this case?

    Thanks,
    Jake


  • Jake Brukhman at Sep 11, 2012 at 3:24 pm
    Here is an example of my intented
    implementation: http://play.golang.org/p/M_GV-qZC90

    We read in the struct, and then translate the [4]byte field into a string.
    On Tuesday, September 11, 2012 11:05:45 AM UTC-4, jorelli wrote:

    ah, ok. Well, a [4]string is an array of four strings, not an array
    describing a string of four characters. That would be [4]rune, but almost
    nothing takes array inputs because an array's size is a part of its type
    ([4]rune and [5]rune are distinct types); you're going to be left working
    with slices length checks to ensure consistency since [4]rune will be an
    almost-certainly meaningless input to any function.

    Passing a rune slice into binary.Read and then converting it to a string
    works.

    Anywho, I found a very ugly solution that my gut tells me must be subtly
    wrong in some way, specifically when handling strings of the wrong size or
    encoding:

    http://play.golang.org/p/bzGRmm7cbf

    It's assumed that you are not the ultimate source of the data. If you
    are, you can save yourself some headache by using the gob package.


    On Tuesday, September 11, 2012 9:07:41 AM UTC-4, Jake Brukhman wrote:

    Hi Jordan!

    Yes, you are right, I should have just passed in "data", not "&data".

    You can simulate the expected input with a buffer or file (e.g.
    io.Reader) that contains two int32's and a four character ASCII string:

    int32(100), int32(101), "HOLA"

    The output would be a Data struct populated with the analogous bytes of
    the above.

    Jake



    On Sep 11, 2012, at 12:01 AM, jorelli wrote:

    I'll do
    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    it's assumed what you mean is:

    var data Data
    binary.Read(someBuf, binary.BigEndian, &data)

    otherwise you just passed **Data into binary.Read instead of *Data

    could you perhaps plop in some sample input with the expected output?
    I'd like to help, but I don't know how to simulate the input offhand.

    something like in the example:

    b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}

    would be killer.

    otherwise if it's just six bytes:

    var data [6]byte
    binary.Read(someBuf, binary.BigEndian, &data)

    one := int32(data[0])
    two := int32(data[1])
    str := string(data[2:])

    I'm not sure if that actually works, I'm just curious to learn about
    binary handling.

    On Monday, September 10, 2012 11:10:30 PM UTC-4, Jake Brukhman wrote:

    Context:

    I have a struct that will be filled in from binary data. I know field
    "str" is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way
    to do the conversion and/or reading in in this case?

    Thanks,
    Jake


  • Jake Brukhman at Sep 11, 2012 at 3:23 pm
    On Monday, September 10, 2012 11:10:30 PM UTC-4, Jake Brukhman wrote:
    Context:

    I have a struct that will be filled in from binary data. I know field
    "str" is going to be a 4-byte string when I read it in.

    type Data struct {
    one int32
    two int32
    str [4]byte
    }

    I'll do

    data := new(Data)
    binary.Read(someBuf, binary.BigEndian, &data)

    and will want to interpret "str" as a string -- but what's the best way to
    do the conversion and/or reading in in this case?

    Thanks,
    Jake


Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 11, '12 at 3:13a
activeSep 11, '12 at 3:24p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase