In this case, the "code golf" solution seems 
clearer: https://play.golang.org/p/Jxkf2Vheml

On Monday, November 13, 2017 at 3:57:57 PM UTC-8, peterGo wrote:
>
> Christian,
>
> Your specialized convertCharToInt32 function, which returns []uint32, is 
> slow in comparison to a more general HexToUint function.
>
> BenchmarkHexToUint32-8   20000000    88.9 ns/op   16 B/op    2 allocs/op
> BenchmarkCharToInt32-8    3000000   438 ns/op     96 B/op   22 allocs/op
>
> Playground: https://play.golang.org/p/OeUDEV9Xpb
>
> Peter
>
> On Monday, November 13, 2017 at 1:51:21 AM UTC-5, 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