[VW 7.1] Binary Coded Decimal

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[VW 7.1] Binary Coded Decimal

mani kartha
Hi ,

Visualworks [7.1 ?] has some library class/method that supports reading and writing of Binary coded decimals ? What do you suggest as a better approach for implementing this requirement?

Another not so important question is,

ByteArray has indexedType #bytes, but its elements are Integer, so why is it not indexed as #objects ? Also, how does smalltalk handle a data of exactly 1 byte? (a ByteArray with only one element?) What is the easiest way to convert it (the byte) to bits?(#printStringRadix:2  ??)

Thanks in advance,

Mani



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [VW 7.1] Binary Coded Decimal

Paul Baumann

Mani,

 

This example creates a ByteArray of the bytes of a double and then converts it back to a double:

 

| d b |

d := 383.323d.

b := ByteArray new: d basicSize.

1 to: b size do: [:i | b at: i put: (d basicAt: i) ].

b changeClassTo: d class.

b

 

383.323d

 

An #object type is is a pointer to another object, and each pointer takes far more than eight bits to implement. Smalltalk handles data of 1 byte by having a ByteArray of size 1 (there is no Byte object just as there is no Bit object). Bit operations are done on integers, and the bytearray representation is a collection of integers. If you need to do bit operations then do them on the integer(s) and not to a string of characters produced from something like #printStringRadix:. If you need to do bit operations on the entire contents then you might use a trick of forming an integer from the contents (instead of the ByteArray in the earlier example):

 

| d b |

d := 383.323d.

b := LargePositiveInteger basicNew: d basicSize.

1 to: b basicSize do: [:i | b digitAt: i put: (d basicAt: i) ].

b

 

4645451105722714554

 

Both ByteArray and LargePositiveInteger are byte structures. LPI just implements behavior that may be more useful to you for bit operations. The #changeClassTo: trick works for LPI instances too. I don't recall if you'd need to be concerned with endian transformations with tricks like these.

 

Paul Baumann

 

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of mani kartha
Sent: Tuesday, March 18, 2014 04:50
To: [hidden email]
Subject: [vwnc] [VW 7.1] Binary Coded Decimal

 

Hi ,

Visualworks [7.1 ?] has some library class/method that supports reading and writing of Binary coded decimals ? What do you suggest as a better approach for implementing this requirement?

Another not so important question is,

ByteArray has indexedType #bytes, but its elements are Integer, so why is it not indexed as #objects ? Also, how does smalltalk handle a data of exactly 1 byte? (a ByteArray with only one element?) What is the easiest way to convert it (the byte) to bits?(#printStringRadix:2  ??)

Thanks in advance,

Mani

 



This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [VW 7.1] Binary Coded Decimal

John Brant-2
In reply to this post by mani kartha

On Mar 18, 2014, at 3:49 AM, mani kartha <[hidden email]> wrote:

> Hi ,
>
> Visualworks [7.1 ?] has some library class/method that supports reading and writing of Binary coded decimals ? What do you suggest as a better approach for implementing this requirement?

I don’t know many places that use BCD numbers, but it is relatively easy to convert BCD bytes to integers:

        | bytes |
        bytes := #[1 2 3 4 5].
        bytes inject: 0 into: [:sum :each | sum * 10 + each]

Integer to BCD bytes:

        | bytes number index |
        number := 12345.
        bytes := ByteArray new: 5. “fix to whatever size you need”
        index := bytes size.
        [number > 0] whileTrue: [
                bytes at: index put: (number \\ 10).
                index := index - 1.
                number := number // 10].
        bytes

Here’s an article that explains the BCD format (and also the Packed BCD format):
        http://en.wikipedia.org/wiki/Binary-coded_decimal


John Brant
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [VW 7.1] Binary Coded Decimal

Steven Kelly
Assuming the same uncompressed BCD as John (i.e. one decimal digit per byte):

(Number readIntegerFrom: '12345' readStream radix: 256) asBigEndianByteArray.
" => #[1 2 3 4 5]  "

#[1 2 3 4 5] asBigEndianInteger printStringRadix: 256.
" => '12345'       "

For packed BCD (i.e. two decimal digits per byte, more common at least in my
experience), John's code is more easily adapted - basically just change the
10s to 100s.

Steve

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of John Brant
> Sent: Tuesday, March 18, 2014 9:21 PM
> To: mani kartha
> Cc: [hidden email]
> Subject: Re: [vwnc] [VW 7.1] Binary Coded Decimal
>
>
> On Mar 18, 2014, at 3:49 AM, mani kartha <[hidden email]> wrote:
>
> > Hi ,
> >
> > Visualworks [7.1 ?] has some library class/method that supports
> reading and writing of Binary coded decimals ? What do you suggest as a
> better approach for implementing this requirement?
>
> I don't know many places that use BCD numbers, but it is relatively
> easy to convert BCD bytes to integers:
>
> | bytes |
> bytes := #[1 2 3 4 5].
> bytes inject: 0 into: [:sum :each | sum * 10 + each]
>
> Integer to BCD bytes:
>
> | bytes number index |
> number := 12345.
> bytes := ByteArray new: 5. "fix to whatever size you
> need"
> index := bytes size.
> [number > 0] whileTrue: [
> bytes at: index put: (number \\ 10).
> index := index - 1.
> number := number // 10].
> bytes
>
> Here's an article that explains the BCD format (and also the Packed BCD
> format):
> http://en.wikipedia.org/wiki/Binary-coded_decimal
>
>
> John Brant
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc