Yes, it looked like a typo to me.

Truncating the string to an even number of characters gives the same 
results as the original snippet:
https://play.golang.org/p/avf6xqnpEn

It's also not difficult to pad the input, if that's the desired behavior:
https://play.golang.org/p/Q6C9SBhQB5
(Padding without copying the whole string would be slightly more complex, 
but as Howard noted, Go style is generally to prefer simpler code.)


On Monday, November 13, 2017 at 5:31:22 PM UTC-8, peterGo wrote:
>
> Bryan,
>
> Use the test case from the question: buffer := "83f982d600c1caca7a6".
>
> https://play.golang.org/p/1gN7Y4ajOH
>
> Peter
>
> On Monday, November 13, 2017 at 7:42:16 PM UTC-5, Bryan Mills wrote:
>>
>> 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