mpm*_*pmp 5 java byte ascii ebcdic ibm-midrange
新来的!
情况:我正在开发一个需要与AS/400服务器通信的项目.我的任务是基本上处理将发送到AS/400服务器的请求.为此,所有用户输入应为EDCDIC字节.
问题:
我已设法将压缩小数转换为String,其代码如下,在此论坛中找到:
public class PackedDecimal {
public static long parse(byte[] pdIn) throws Exception {
// Convert packed decimal to long
final int PlusSign = 0x0C; // Plus sign
final int MinusSign = 0x0D; // Minus
final int NoSign = 0x0F; // Unsigned
final int DropHO = 0xFF; // AND mask to drop HO sign bits
final int GetLO = 0x0F; // Get only LO digit
long val = 0; // Value to return
for (int i = 0; i < pdIn.length; i++) {
int aByte = pdIn[i] & DropHO; // Get next 2 digits & drop sign bits
if (i == pdIn.length - 1) { // last digit?
int digit = aByte >> 4; // First get digit
val = val * 10 + digit;
// System.out.println("digit=" + digit + ", val=" + val);
int sign = aByte & GetLO; // now get sign
if (sign == MinusSign)
val = -val;
else {
// Do we care if there is an invalid sign?
if (sign != PlusSign && sign != NoSign)
throw new Exception("OC7");
}
} else {
int digit = aByte >> 4; // HO first
val = val * 10 + digit;
// System.out.println("digit=" + digit + ", val=" + val);
digit = aByte & GetLO; // now LO
val = val * 10 + digit;
// System.out.println("digit=" + digit + ", val=" + val);
}
}
return val;
} // end parse()
// Test the above
public static void main(String[] args) throws Exception {
byte[] pd = new byte[] { 0x19, 0x2C }; // 192
System.out.println(PackedDecimal.parse(pd));
pd = new byte[] { (byte) 0x98, 0x44, 0x32, 0x3D }; // -9844323
System.out.println(PackedDecimal.parse(pd));
pd = new byte[] { (byte) 0x98, 0x44, 0x32 }; // invalid sign
System.out.println(PackedDecimal.parse(pd));
}
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是我必须将这些String值再次转换为EBCDIC字节,以便AS/400服务器能够理解它.我打算使用Silverlake文档中指定的格式来构建请求(原始字节).一旦构建了请求,我计划使用存储我的请求的POJO手动更改该请求中的值(使用setter和getter),这样我就可以了request.setField1("Stuff".getBytes(Charset.forName("Cp1047")))
.
我对bit,bytes和半字节没有那么多经验.我希望有人可以帮助我.
在我们的代码中,我们发现了一个由5个字节组成的压缩十进制数.它类似于= {00 00 00 00 0F}.我使用从上面的代码获得的方法转换它,我得到的值是0.现在,我想将此0转换回其原始字节大小为5的原始形式.
这是我的长整型十进制方法的版本。
public class PackedDecimal {
public static byte[] format(long number, int bytes) {
byte[] b = new byte[bytes];
final byte minusSign = 0x0D; // Minus
final byte noSign = 0x0F; // Unsigned
String s = Long.toString(number);
int length = s.length();
boolean isNegative = false;
if (s.charAt(0) == '-') {
isNegative = true;
s = s.substring(1);
length--;
}
int extraBytes = length - bytes + 1;
if (extraBytes < 0) {
// Pad extra byte positions with zero
for (int i = 0; i < -extraBytes; i++) {
b[i] = 0x00;
}
} else if (extraBytes > 0) {
// Truncate the high order digits of the number to fit
s = s.substring(extraBytes);
length -= extraBytes;
extraBytes = 0;
}
// Translate the string digits into bytes
for (int i = 0; i < length; i++) {
String digit = s.substring(i, i + 1);
b[i - extraBytes] = Byte.valueOf(digit);
}
// Add the sign byte
if (isNegative) {
b[bytes - 1] = minusSign;
} else {
b[bytes - 1] = noSign;
}
return b;
}
public static void main(String[] args) {
long number = -456L;
byte[] b = PackedDecimal.format(number, 5);
System.out.println("Number: " + number + ", packed: " + byteToString(b));
number = 0L;
b = PackedDecimal.format(number, 5);
System.out.println("Number: " + number + ", packed: " + byteToString(b));
number = 5823L;
b = PackedDecimal.format(number, 5);
System.out.println("Number: " + number + ", packed: " + byteToString(b));
number = 123456L;
b = PackedDecimal.format(number, 5);
System.out.println("Number: " + number + ", packed: " + byteToString(b));
}
public static String byteToString(byte[] b) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < b.length; i++) {
sb.append("0x");
sb.append(Integer.toHexString((int) b[i]).toUpperCase());
sb.append(" ");
}
return sb.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试结果。
Number: -456, packed: 0x0 0x4 0x5 0x6 0xD
Number: 0, packed: 0x0 0x0 0x0 0x0 0xF
Number: 5823, packed: 0x5 0x8 0x2 0x3 0xF
Number: 123456, packed: 0x3 0x4 0x5 0x6 0xF
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14636 次 |
最近记录: |