在JAVA中如何将字符串转换成压缩的BCD码? String s = "B510610E627D9E5A"; 在JAVA中如何将字符串转换成压缩的BCD码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 void BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len){ int i; char ch; for (i=0; i<len; i++) { if (i & 1) ch = *(bcd_buf++) & 0x0f; else ch = *bcd_buf >> 4; ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0'); }} 请高手看看我写的代码有没有问题?生成的BCD码是不是正确的? public static byte[] AcsToBcd(String asc) { int len = asc.length(); int mod = len % 2; if (mod != 0) { asc = "0" + asc; len = asc.length(); } byte abt[] = new byte[len]; if (len >= 2) { len = len / 2; } byte bbt[] = new byte[len]; abt = asc.getBytes(); int j, k; for (int p = 0; p < asc.length()/2; p++) { if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { j = abt[2 * p] - '0'; } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { j = abt[2 * p] - 'a' + 0x0a; } else { j = abt[2 * p] - 'A' + 0x0a; } if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { k = abt[2 * p + 1] - '0'; } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { k = abt[2 * p + 1] - 'a' + 0x0a; } else { k = abt[2 * p + 1] - 'A' + 0x0a; } // int a = (j << 4) + k; byte b = (byte) a; bbt[p] = b; } return bbt; } 请解释一下BSD的概念.---初学者 public class T{ public static byte[] ByteToBCD(byte[] in) { byte[] out = new byte[in.length * 2]; int tmp; for(int i=0; i< in.length * 2; i++) { tmp = (in[i >> 1 ] >> ((i + 1) % 2)*4) & 0x0f ; switch(tmp) { case 10: out[i] = 'A'; break; case 11: out[i] = 'B'; break; case 12: out[i] = 'C'; break; case 13: out[i] = 'D'; break; case 14: out[i] = 'E'; break; case 15: out[i] = 'F'; break; default : out[i] = (byte)(tmp|0x30); } } return out; } public static void main(String[] args) { // 字符串这样传入 //String src1 = "B510610E627D9E5A"; //byte[] des = ByteToBCD(src.getBytes()); byte[] src2 = {'1','2',(byte)0x1E}; byte[] des = ByteToBCD(src); for (int i = 0; i < des.length; i ++) System.out.print((char)des[i] + " "); }} 关于socket编程的一个问题。 eclipse 用户库的问题 编程题。求解 如何根据月份生成相应的期号? 关于FileInputStream声明的问题 Java Media Frame 在Applet中的应用 如何在JButton或JLabel中设置图标的大小(源图是大图)想设置成工具条按钮大小的? 请教一个Java代码的错误的调试解决方法 如何随机取出0,1,3这三个数的其中一个 List集合add后还是空 求助高手,这个记录网页访问次数代码错在哪里? 关于继承和构造器的问题
{
int i;
char ch;
for (i=0; i<len; i++)
{
if (i & 1) ch = *(bcd_buf++) & 0x0f;
else ch = *bcd_buf >> 4;
ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
}
}
public static byte[] AcsToBcd(String asc) { int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k; for (int p = 0; p < asc.length()/2; p++) {
if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
}
else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
}
else {
j = abt[2 * p] - 'A' + 0x0a;
} if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
}
else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
}
else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
//
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
{
public static byte[] ByteToBCD(byte[] in)
{
byte[] out = new byte[in.length * 2];
int tmp;
for(int i=0; i< in.length * 2; i++)
{
tmp = (in[i >> 1 ] >> ((i + 1) % 2)*4) & 0x0f ; switch(tmp)
{
case 10: out[i] = 'A'; break;
case 11: out[i] = 'B'; break;
case 12: out[i] = 'C'; break;
case 13: out[i] = 'D'; break;
case 14: out[i] = 'E'; break;
case 15: out[i] = 'F'; break;
default :
out[i] = (byte)(tmp|0x30);
}
}
return out;
}
public static void main(String[] args)
{
// 字符串这样传入
//String src1 = "B510610E627D9E5A";
//byte[] des = ByteToBCD(src.getBytes());
byte[] src2 = {'1','2',(byte)0x1E};
byte[] des = ByteToBCD(src);
for (int i = 0; i < des.length; i ++)
System.out.print((char)des[i] + " ");
}
}