如何用AS400ZonedDecimal将正BigDecimal转为带符号EBCDIC数?
AS400ZonedDecimal 正数转带符号EBCDIC数的解决方法
先看你的代码场景:
AS400ZonedDecimal converter = new AS400ZonedDecimal(3, 0); byte[] convertedBytesArray = converter.toBytes(new BigDecimal("-120"));
转换负数-120时得到'12}'的字节数组是符合预期的,但转正数120得到'120',和你期望的'12{'不符。
解决办法很直接,初始化AS400ZonedDecimal时要显式指定带符号模式:
// 第三个参数传入AS400ZonedDecimal.SIGNED,强制使用带符号的zoned decimal格式 AS400ZonedDecimal converter = new AS400ZonedDecimal(3, 0, AS400ZonedDecimal.SIGNED); byte[] positiveBytes = converter.toBytes(new BigDecimal("120"));
原因是默认构造的AS400ZonedDecimal对正数采用无符号处理,最后一位保留原数字;而指定SIGNED模式后,正负数值都会用符号位替换最后一位数字——正数对应EBCDIC的{,负数对应},完全匹配你的需求。
内容的提问来源于stack exchange,提问作者Eric Lee
相关产品推荐
相关产品推荐

