How to parse packed bcd bytes in golang?

1,274 views
Skip to first unread message

di3go.b...@gmail.com

unread,
Nov 27, 2014, 1:08:31 PM11/27/14
to golan...@googlegroups.com
hi guys!
I need to implement a tracker protocol and in one of the field i have to do this:

The id is made up of numerical digits and is encoded in this field as packed BCD. The most significant digit is placed in the upper four bits of the first byte. In the case where an odd number of digits are used, a 0x0F is used to pad the lower 4 bits of the last byte.

How can i parse this []byte to a string in golang?
Thanks!!

Matt Harden

unread,
Nov 27, 2014, 1:44:58 PM11/27/14
to di3go.b...@gmail.com, golan...@googlegroups.com
http://godoc.org/encoding/hex#EncodeToString. If you actually want an int, you can remove any trailing 'f' from the string (if the length is odd) and use http://godoc.org/strconv#Atoi.

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Skip Tavakkolian

unread,
Nov 27, 2014, 1:52:48 PM11/27/14
to di3go.b...@gmail.com, golang-nuts
maybe something like this: http://play.golang.org/p/qzqVtCR0C4

--

chris dollin

unread,
Nov 27, 2014, 1:57:42 PM11/27/14
to Matt Harden, di3go.b...@gmail.com, golang-nuts
On 27 November 2014 at 18:55, chris dollin <ehog....@googlemail.com> wrote
> You just pick the bytes apart into 4-bit chunks and do a shift-and-add
> to build up a number.

MULTIPLY (by 10) and add.

Grrr, argh.

Chris

--
Chris "allusive" Dollin

chris dollin

unread,
Nov 27, 2014, 1:58:14 PM11/27/14
to Matt Harden, di3go.b...@gmail.com, golang-nuts
On 27 November 2014 at 18:44, Matt Harden <matt....@gmail.com> wrote:
> http://godoc.org/encoding/hex#EncodeToString. If you actually want an int,
> you can remove any trailing 'f' from the string (if the length is odd) and
> use http://godoc.org/strconv#Atoi.

packed BCD isn't hex.

> On Thu Nov 27 2014 at 12:08:40 PM <di3go.b...@gmail.com> wrote:
>>
>> hi guys!
>> I need to implement a tracker protocol and in one of the field i have to
>> do this:
>>
>> The id is made up of numerical digits and is encoded in this field as
>> packed BCD. The most significant digit is placed in the upper four bits of
>> the first byte. In the case where an odd number of digits are used, a 0x0F
>> is used to pad the lower 4 bits of the last byte.
>>
>> How can i parse this []byte to a string in golang?

Are you sure you want to parse it to a string and not, say, an int64?

You just pick the bytes apart into 4-bit chunks and do a shift-and-add
to build up a number.

Matt Harden

unread,
Nov 27, 2014, 2:00:55 PM11/27/14
to chris dollin, di3go.b...@gmail.com, golang-nuts
Packed BCD isn't hex, but it is equivalent to taking a decimal expansion then encoding as hex. Try it; it works.

Matt Harden

unread,
Nov 27, 2014, 2:12:48 PM11/27/14
to chris dollin, di3go.b...@gmail.com, golang-nuts
If you don't believe me check decodeBcd2 in http://play.golang.org/p/kZf3xWMjR8.

di3go.b...@gmail.com

unread,
Nov 27, 2014, 3:00:31 PM11/27/14
to golan...@googlegroups.com, ehog....@googlemail.com, di3go.b...@gmail.com
Thanks guys!
Both solutions work, but bcd is faster than bcd2.

go test -bench=. 
PASS
BenchmarkBCD 100000000        23.8 ns/op
BenchmarkBCD2 5000000       326 ns/op

Matt Harden

unread,
Nov 27, 2014, 11:29:42 PM11/27/14
to di3go.b...@gmail.com, golan...@googlegroups.com, ehog....@googlemail.com
Yes, but bcd2 is simpler and more obviously correct if you know what the underlying functions do.

--

Matt Harden

unread,
Nov 28, 2014, 12:36:04 AM11/28/14
to di3go.b...@gmail.com, golan...@googlegroups.com, ehog....@googlemail.com
decodeBcd was handling overflows incorrectly.

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Skip Tavakkolian

unread,
Nov 28, 2014, 3:29:47 AM11/28/14
to di3go.b...@gmail.com, golang-nuts
ok what i sent is clearly not correct. i shouldn't try to code and cook a turkey at the same time.
to redeem myself here's a version that is correct (i think): http://play.golang.org/p/ChmTmCtkHq

-Skip
Reply all
Reply to author
Forward
0 new messages