wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Convert from ASCII to EBCDIC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply locked New topic
Author

Convert from ASCII to EBCDIC

Raji Manne
Greenhorn

Joined: Sep 23, 2009
Posts: 3
Hi,

Requirement is : Once user enters a decimal number , it should be stored in Db2 as PAcked Decimal (COMP3)
Application is running on AIX(Encoding : ASCII) and DB2 is in Mainframe(Encoding : EBCDIC )
JDK Version: 1.5

The follwoing logic is used to convert decimal to PACKEDDEIMAL and then to EBCDIC Encoding.

Problem is : When the number "154.10" is converted to EBCDIC it is stored as 254.10. When 154.10 and 254.10 ae converted, we are getting the same bytes after encoding (for both numbers). For other numbers it is working fine. This problem is occurrent when 1 followed by 5 (For Ex: 154.10, 451.56 , etc..). Please advise if there is any problem with JDK1.5 encoding or Is there any other way to convert deciaml numbers into Packed decimal format?



package com.test.data;

public class TestPackDec1 {


/**
* @param args
*/
public static void main(String[] args) throws Exception{

String number = "154.10";

System.out.println(" Covert "+number+ " Using Packed Decimalm PackDec class ************");

byte[] buffr1 = new byte[6];

stringToPack(number, buffr1, 0, 6 );

System.out.println("Packed decimal Bytes " );
printByte(buffr1);

String encodedStr = new String(buffr1, "Cp037");
System.out.println("Encoded String " +encodedStr);
System.out.println("Encoded String Bytes " );
printByte(encodedStr.getBytes());

System.out.println("Encoded String Bytes2 Cp037 ");
printByte(encodedStr.getBytes("Cp037"));


}

/**
* Converts String to packed decimal number. Decimal points, commas and
* spaces are ignored. Sign character is processed. Avoid multiple signs.
* Characters other than digits are invalid and will cause DataException.
* Comma, blank, period, dollar sign and plus are ignored. Scaling and
* exponents are not valid.
*
* @param str
* String of number to convert
* @return byte array of packed decimal, 16 long
*/
public static byte[] stringToPack(String str) throws Exception {
int i; // string index
int j; // byte array index
boolean nibble_ordinal = false;
char ch1;
byte nibble;
int strlen = str.length();
byte[] pknum = new byte[16];
for (i = 0; i < 16; i++) { // initialize to zero
pknum[i] = 0;
}
i = strlen - 1;
j = 15; /* byte index */
pknum[j] = 12; // start with positive sign
while (i > -1) {
ch1 = str.charAt(i);
switch (ch1) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
nibble = (byte) Character.getNumericValue(ch1);
if (nibble_ordinal) {
pknum[j] = (byte) (pknum[j] | nibble);
nibble_ordinal ^= true;
} else {
pknum[j] = (byte) (pknum[j] | nibble << 4);
nibble_ordinal ^= true;
--j;
}
--i; // get next char
break;
case ',':
case ' ':
case '.':
case '$':
case '+':
--i; // get next char
break;
case '-':
pknum[15] = (byte) (pknum[15] & 0xf0);
pknum[15] = (byte) (pknum[15] | 0x0d);
--i; // get next char
break;
default:
throw new Exception("Data Exception : Invalid decimal digit: " + ch1);
}
}
return (pknum);
}

/**
* Convenience method to convert a String to packed decimal. The packed
* number is stored in an existing array.
*
* @param str
* Number to be converted
* @param bytearray
* Contains result
* @param offset
* Location in array
* @param len
* Number of bytes of result
* @throws DecimalOverflowException
* If result is larger than result length
*/
public static void stringToPack(String str, byte[] bytearray, int offset,
int len) throws Exception {
byte[] pknum = stringToPack(str); /* get packed num of length 16 */
int pklen = pknum.length;
if (len < pklen) // check to see of number too big
if (pknum[pklen - len - 1] != 0)
throw new Exception("DecimalOverflowException : Number too big");
int i = offset + len - 1;
int j = pklen - 1;
int k;
for (k = 0; k < len; k++) {
bytearray[i] = pknum[j];
--i;
--j;
}
}

public static void printByte(byte[] bytearr){
if (bytearr != null) {
for (int i=0; i < bytearr.length; i++){
System.out.print(bytearr[i] + " ");
}
System.out.println();
}
}





}

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags. Unformatted code/config/etc. is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't understand why you are doing any of that. Your JDBC driver should take care of all of it for you.
Raji Manne
Greenhorn

Joined: Sep 23, 2009
Posts: 3
Paul Clapham wrote:I don't understand why you are doing any of that. Your JDBC driver should take care of all of it for you.


Yes.. JDBC Driver should take care f this.. But this decimal is part of a big string (Amount is in some 100th position). I tried without encoding.. But it is stored as different number.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Like David, I was stalling in the hope that you would edit your post to use the code tags. You haven't done that yet and therefore your code is still unreadable. Please post again when you have some readable code for us to look at.l
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Note that a duplicate of this question has been posted here: http://www.coderanch.com/t/464119/Other-JSE-JEE-APIs/java/Convert-from-ASCII-EBCDIC.

I will close this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert from ASCII to EBCDIC
 
Similar Threads
problem base64 encoding the 3 hex bytes E2 80 A9
Using Comp-3 value in Java
Basic Authentication Servlet Redirect
Packed decimal conversion - Java
Convert from ASCII to EBCDIC