• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issues with PackedDecimal class for lower values  RSS feed

 
Sidhartha Priye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

If you are out there and reading this when I pass a decimal 1 to your Packed Decimal class it returns me the following - 00 00 00 1F...however all I am expecting is 00 00 00 01. I looked at your code and you append a F or a D to the right nibble of the first byte. Could you add/modify the code for lower numbers so that you just pass back the hex value of 1 distributed across 4 bytes for an integer or 8 bytes for a long.

Let me know if my question makes sense.

thanks
Sid

Attached your code for instant reference

public class PackedDecimal
{
private byte[] mBytes;
private int mInt;
private static final byte POSITIVE = 0x0f;
private static final byte NEGATIVE = 0x0d;

public PackedDecimal(int aInt, int aByteCount)
{
mInt = aInt;
mBytes = new byte[aByteCount];
int lByteIx = aByteCount - 1;
// right nibble of first byte is the sign
if (aInt >= 0)
mBytes[lByteIx] = POSITIVE;
else
{
mBytes[lByteIx] = NEGATIVE;
aInt = -aInt;
}
// left nibble of the first byte is the ones
int lDigit = aInt % 10;
mBytes[lByteIx] |= (lDigit << 4);
while (lByteIx > 0)
{
// next byte over
lByteIx--;
// right nibble
aInt = aInt / 10;
lDigit = aInt % 10;
mBytes[lByteIx] |= (lDigit);
// left nibble
aInt = aInt / 10;
lDigit = aInt % 10;
mBytes[lByteIx] |= (lDigit << 4);
}
}

public PackedDecimal(byte[] aBytes)
{
mBytes = aBytes;
mInt = 0;

int lByteIx = aBytes.length - 1;
mInt += aBytes[lByteIx] >> 4;

int lFactor = 10;
while (lByteIx > 0)
{
lByteIx--;
mInt += ((aBytes[lByteIx] & 0x0f) * lFactor);
lFactor *= 10;
mInt += ((aBytes[lByteIx] >> 4) * lFactor);
lFactor *= 10;
}
if ((aBytes[aBytes.length-1] & 0x0f) == NEGATIVE)
mInt *= -1;
}

public int toInt()
{
return mInt;
}
public byte[] toByteArray()
{
return mBytes;
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

I made that PackedDecimal from my memory of COBOL comp-3 fields. They always have the sign in the rightmost nibble. One source I just googled used C and D for signs where I used F and D, but that code is kinda close.

I think what you're looking for is Binary Coded Decimal. I bet you could modify that code to make BCD by removing the lines for the rightmost byte and doing the next loop for the whole input.
 
Sidhartha Priye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan - Yes I am looking for an unsigned packed decimal as my output.

I took a dig myself and came up with this but I am missing something here...
Let me first include my code

My input is a number 31581 Base 10
Now I want to write the output in HEX...so I invoke the above method
like this


where 4 is the fieldLth or 4 bytes for an integer. I was expecting an output of 00 00 7B 5D ( in hex)...instead of I got this 00 00 72 54.

Can you or my friends in java ranch point out where I am going wrong???
 
Sidhartha Priye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

Finally figured it out. My code is only meant to work for ASCII base 10 numbers. So I thought I will modify your code to return HEX values.

My requirements
1. Return unsigned packed numbers
2. Returns Hex Values from a given int base 10.

Modifications :
1. Ignore sign bit on the rightmost nibble.
2. Process the entire input inside the loop.
3. Parse the int (% and /) on base 16 instead of base 10...so the numbers automatically gets stored as hex



and the call to this



The "mBytes" returns me unsigned Packed Hex as 00 00 7b 5d. Use DataOutputStream object (for eg. dos) to write your bytes to the file.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!