Scodec : Decoding Packed-Decimal

63 views
Skip to first unread message

Olivier Girardot

unread,
Jul 30, 2015, 10:49:43 AM7/30/15
to Typelevel Users & Development List
Hi everyone (this is copied from the scodec mailing-list), 
I'm trying to decode packed-decimal (c.f. https://en.wikipedia.org/wiki/Binary-coded_decimal ) 
One simple example is something like that : 

   println(Codec.decode(hex"9321".bits)(uint4 ~~ uint4 ~~ uint4 ~~ uint4))

with the result being : 

   Successful(DecodeResult((9,3,2,1),BitVector(empty)))

I would like to be able to create a Codec or helper function where I can only specify the number of 4-bits, I want to parse and get the associated Long.
Like that : 

   > println(Codec.decode(hex"9321".bits)(packedDecimalCodec(4)))
   Successful(DecodeResult((9321),BitVector(empty)))

I'm kind of stuck here, and I'd really appreciate your help.

Regards, 

Olivier.

chris...@gmail.com

unread,
Jul 30, 2015, 3:06:59 PM7/30/15
to Typelevel Users & Development List, ssa...@gmail.com
I did something similar to this here.  It differs slightly in that I'm also decoding the number of times to run a given codec from the input bytes.  If you remove the size encoding/decoding stuff, it should do what you want.

Olivier Girardot

unread,
Jul 30, 2015, 3:16:58 PM7/30/15
to chris...@gmail.com, Typelevel Users & Development List
Nice, Thanks !
I didn't notice the decoding context, any other snippet or examples, that I could leverage to better get my hands around this ?

Regards, 

Olivier.

Michael Pilquist

unread,
Jul 30, 2015, 3:31:06 PM7/30/15
to chris...@gmail.com, Olivier Girardot, Typelevel Users & Development List
By the way, just last night I deprecated DecodingContext, as ever since the refactor in 1.7.0, it is completely redundant with Decoder’s flatMap method, which Codec inherits. So for example, code like this:

(for {
  a <- DecodingContext(codecA)
  b <- DecodingContext(codecB)
} yield a + b).decode(bits)

Is equivalent to:

(for {
  a <-codecA
  b <-codecB
} yield a + b).decode(bits)

Cheers,
Michael
--
You received this message because you are subscribed to the Google Groups "Typelevel Users & Development List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to typelevel+...@googlegroups.com.
To post to this group, send email to type...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/typelevel/CAAGLpn419ESYogQJ%2B1NMT60PAdkEpifDrr3g_R9Cf0TCf9voCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

chris...@gmail.com

unread,
Jul 30, 2015, 4:03:23 PM7/30/15
to Typelevel Users & Development List, ssa...@gmail.com
I learned about the decoding context from here.   Even though the decoding context is going away, apparently, you still might find it useful. Working through the 3 parts of this tutorial made working with scodec much easier for me.

Olivier Girardot

unread,
Jul 30, 2015, 5:08:04 PM7/30/15
to chris...@gmail.com, Typelevel Users & Development List
So to decode "n" times a uint4 
I guess I'd just need to do as much for comprehension ?
Reply all
Reply to author
Forward
0 new messages