求大牛帮个忙!!!写个BCD码转char字符的算法!!!
不胜感谢。
/**
* 从BCD编码转换成ASCII编码.
*
* @param bcdBuf
* , BCD编码缓冲区
* @param asciiLen
* , 统一采用ASCII编码时的信息长度
* @param rightAlignFlag
* , 奇数个ASCII码时采用的右对齐方式标志
* @return, ASCII编码缓冲区
*/
public static byte[] bcd2Ascii(byte[] bcdBuf, int bcdOffset, int asciiLen, boolean rightAlignFlag) {
byte[] asciiBuf = new byte[asciiLen];
int cnt;
if (((asciiLen & 1) == 1) && rightAlignFlag) {
cnt = 1;
asciiLen++;
}
else {
cnt = 0;
}
int asciiOffset = 0;
for (; cnt < asciiLen; cnt++, asciiOffset++) {
asciiBuf[asciiOffset] =
(byte) ((((cnt) & 1) == 1) ? (bcdBuf[bcdOffset++] & 0x0f) : ((bcdBuf[bcdOffset] >> 4) & 0x0f));
asciiBuf[asciiOffset] =
(byte) (asciiBuf[asciiOffset] + ((asciiBuf[asciiOffset] > 9) ? (ALPHA_A_ASCII_VALUE - 10)
: DIGITAL_0_ASCII_VALUE));
}
return asciiBuf;
}
/**
* 从BCD编码转换成ASCII编码字符串
*
* @param bcdBuf
* , BCD编码缓冲区
* @param asciiLen
* , 统一采用ASCII编码时的信息长度
* @param rightAlignFlag
* , 奇数个ASCII码时采用的右对齐方式标志
* @return, ASCII编码的字符串
*/
public static String bcd2AsciiString(byte[] bcdBuf, int bcdOffset, int asciiLen, boolean rightAlignFlag,
String encoding) {
try {
return new String(bcd2Ascii(bcdBuf, bcdOffset, asciiLen, rightAlignFlag), encoding);
}
catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}