Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.help Subject: Re: Help with BCD conversion Date: Sat, 18 Feb 2012 10:25:51 -0800 (PST) Organization: http://groups.google.com Lines: 51 Message-ID: <31483437.61.1329589551120.JavaMail.geo-discussion-forums@pbcr5> References: <92b642e7-0aa8-4e1a-9e4e-9418bfcc8123@k40g2000yqf.googlegroups.com> <8motj7ljbcapmd0i3kho2g7een4egail5m@4ax.com> <5dlvj79ek56vcmqthh0cudaub6hhc78tsn@4ax.com> NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1329589656 10756 127.0.0.1 (18 Feb 2012 18:27:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 18 Feb 2012 18:27:36 +0000 (UTC) In-Reply-To: <5dlvj79ek56vcmqthh0cudaub6hhc78tsn@4ax.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1583 Roedy Green wrote: > The original IBM 360 style packed two decimal digits per nibble into > an 8 bit byte. It was stored big-endian. The last nibble encoded the That's BCD packed. There's also BCD unpacked. > sign, using one of the hex digits A-F. A, C, E, and F indicate > positive values, and B and D indicate negative values. Seems to me C, > D and F were preferred. > > IIRC there was no explicit length encoding. That was either > determined in the instruction code or by banging into the sign nibble. There's no explicit length encoding in Java's 'BigDecimal' either. There's no reason to expect one. > If there were an even number of digits, it was padded out with a left > zero. > > OP wants to simulate this on Java without the sign. It could be done > with a String, left zero padded to an even number of digits, which is > a trivial problem. It could be done with a byte array, two digits per > byte. The end result could be an int or long. You could start with a > String, int or long. I wanted OP to be aware of the possible choices > and pick the best for his purposes before nailing down code. > > This nibble packing is very similar to converting a Hex string to an > int. > > The core packing code is something like this > int lowNibble = ... > int highNibble = ... > int combinedbyte = highNibble << 8 | lowNibble; > > You probably don't want to use the byte type because it sign extends. > I have yet to find a need for signed bytes. I wish Java had made > bytes unsigned. It would have saved so many hair follicles. That's not a reason not to use the byte type. You just have to compensate for it. Don't throw the baby out with the bath water. > The code OP presented with case clauses is fundamentally incorrect in > that it treats each of the cases as different. They are not, and > should be handled by the same code.I think that is mainly why everyone > has been reluctant to delve into it. Amen to that. -- Lew