Hi Christian, 
just a form note - it would be preferable and easier for the readers to put 
a code snippet into https://play.golang.org and share the link, similar to 
this one:
https://play.golang.org/p/p3TNDze923

It also helps to make sure that program is syntactically correct (not clear 
in your example what "Endian" refers to - binary.LittleEndian?) and the 
example can be run in the Playground.

Take a look at examples and API docs in 
https://golang.org/pkg/encoding/binary/ - they should cover what the stdlib 
provides for conversions.

On Monday, November 13, 2017 at 7:51:21 AM UTC+1, Christian LeMoussel wrote:
>
> I have a data stream of bytes and I'd like to get array of int32 (from 
> four bytes).
>
> func convertCharToInt32(buffer string) []uint32 {
>     const SIZEOF_INT32 = 4
>
>     var hh = make([]byte, 2)
>     var cbuffer = make([]byte, len(buffer)/2)
>     var hbuffer = make([]uint32, len(cbuffer)/SIZEOF_INT32)
>
>     for i := 0; i < 28; i++ {
>         hh[0] = buffer[i*2]
>         hh[1] = buffer[i*2+1]
>         if s, err := strconv.ParseUint(string(hh[:]), 16, 64); err == nil 
> {
>             cbuffer[i] = byte(s)
>         }
>     }
>
>     for i := range hbuffer {
>         hbuffer[i] = uint32(Endian.Uint32(cbuffer[i*SIZEOF_INT32 : (i+1)*
> SIZEOF_INT32]))
>     }
>
>     return hbuffer
> }
>
> buffer := "83f982d600c1caca7a6"
> hbuffer := convertCharToInt32(buffer)
>
>
>
> The code above seems to work, but perhaps there is a built-in function in 
> Go that I've missed or there is a super cool hack that does that in one 
> instruction?
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to