ASC与BCD码之间的转换

uglyFairy 2006-03-23 11:42:56
代码如下:

str2Bcd可以处理阿拉伯数字和英文字母

bcd2Str可以处理阿拉伯数字不能处理英文字母

问题:
str2Bcd的处理正确吗?如果错误,错在那里?
bcd2Str的处理正确吗?如果错误,错在那里? 还有怎么实现处理英文字母?

-----------------------------------------------------------------------------
package Lib.AscBcd;

import java.nio.*;

/**
* @功能: ASC与BCD码的转化
* @生产者: 史建敏
* @生产时间: 2006-3-23
*/
public class AscBcd {

/**
* @功能: 构造体
*/
public AscBcd() {
}

/**
* @param args
*/
public static void main(String[] args)
{
String sTestData = "1234567890";
System.out.println("原数据:" + sTestData);
System.out.println("Bcd:" + str2Bcd(sTestData));
System.out.println("ASC:" + bcd2Str(str2Bcd(sTestData)));

sTestData = "B510610E627D9E5A";
System.out.println("原数据:" + sTestData);
System.out.println("Bcd:" + str2Bcd(sTestData));
System.out.println("ASC:" + bcd2Str(str2Bcd(sTestData)));
}

/**
* @函数功能: BCD码转为10进制串(阿拉伯数据)
* @输入参数: BCD码
* @输出结果: 10进制串
*/
public static String bcd2Str(byte[] bytes){
StringBuffer temp=new StringBuffer(bytes.length*2);

for(int i=0;i<bytes.length;i++){
temp.append((byte)((bytes[i]& 0xf0)>>>4));
temp.append((byte)(bytes[i]& 0x0f));
}
return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
}

/**
* @函数功能: 10进制串转为BCD码
* @输入参数: 10进制串
* @输出结果: BCD码
*/
public static byte[] str2Bcd(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;
}
}
...全文
1366 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
dztc 2006-03-26
  • 打赏
  • 举报
回复
mark
wizardblue 2006-03-26
  • 打赏
  • 举报
回复
本来就只能处理0123456789 abcdef 的
uglyFairy 2006-03-26
  • 打赏
  • 举报
回复
TO :wizardblue(不死鱼)
KL用你的代码不能处理啊,是不是ASC与BCD码之间转换的字符集仅是0123456789 abcdef ,其他的字符不能处理是吗?
wizardblue 2006-03-25
  • 打赏
  • 举报
回复
A 和 a都是映射到0xoa,所以转成0x0a根本分不出是A还是a转过来的
wizardblue 2006-03-25
  • 打赏
  • 举报
回复
怎么不对了?
uglyFairy 2006-03-25
  • 打赏
  • 举报
回复
TO :wizardblue(不死鱼)
处理结果:
原数据:ABCDEF0123456789abcdef
strToAscii:10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
asciiToBcd:-85 -51 -17 1 35 69 103 -119 -85 -51 -17
bcdToAscii:10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
asciiToStr:abcdef0123456789abcdef

处理结果不太对
原数据:ABCDEF
asciiToStr:abcdef
wizardblue 2006-03-24
  • 打赏
  • 举报
回复
附上我的代码


import java.security.InvalidParameterException;

public class ASCII2BCD {

public final static char[] ascii = "0123456789abcdef".toCharArray();

public byte[] bcd2Ascii(byte[] bytes) {
byte[] temp = new byte[bytes.length *2];

for (int i = 0; i < bytes.length; i++) {
temp[i*2]=(byte)((bytes[i] >> 4)&0x0f);
temp[i*2+1]=(byte) (bytes[i] & 0x0f);

}
return temp;
}

public byte[] str2Ascii(String s){
byte[] str = s.toLowerCase().getBytes();
byte[] ascii = new byte[str.length];
for(int i=0;i<ascii.length;i++){
ascii[i]=(byte)asciiValue(str[i]);
}
return ascii;
}
public String ascii2Str(byte[] ascii){
StringBuffer res = new StringBuffer();
for(int i=0;i<ascii.length ;i++){
res.append(strValue(ascii[i]));
}
return res.toString() ;
}
private char strValue(byte asc){
if(asc<0||asc>15)throw new InvalidParameterException();
return ascii[asc];
}

private String bin(int i){
return Integer.toBinaryString(i);
}

public byte[] ascii2Bcd(byte[] asc) {
int len = asc.length / 2;
byte[] bcd = new byte[len];
for (int i = 0; i < len; i++) {
bcd[i] = (byte) ((asc[2 * i] << 4) | asc[2 * i + 1]);
}
return bcd;
}

private int asciiValue(byte b) {
if ((b >= '0') && (b <= '9')) {
return (b - '0');
}
if((b >= 'a') && (b <= 'f')) {
return ( b - 'a' )+ 0x0a;
}
if((b >= 'A') && (b <= 'F')) {
return ( b - 'A' )+ 0x0a;
}

throw new InvalidParameterException();
}



public static void show(byte[] b){
for(int i=0;i<b.length ;i++){
System.out.print(b[i]+" ");
}
System.out.println();

}
static String [] sTestData = new String[]{ "ABCDEF0123456789abcdef"};
public static void main(String[] args) {
ASCII2BCD t = new ASCII2BCD();
for(int i=0;i<sTestData.length ;i++){
System.out.println("原数据:");
System.out.println(sTestData[i]);
System.out.println("strToAscii:");
byte[] ascii = t.str2Ascii (sTestData[i]);
show(ascii);
System.out.println("asciiToBcd:");
byte[] bcd = t.ascii2Bcd (ascii);
show(bcd);
System.out.println("bcdToAscii:");
ascii = t.bcd2Ascii( bcd);
show(ascii);
System.out.println("asciiToStr:");
String str = t.ascii2Str (ascii);
System.out.println(str);
}
}



}
wizardblue 2006-03-24
  • 打赏
  • 举报
回复
刚才看了一下,感觉楼主的思路有点混乱,
ascii->bcd
bcd->ascii
ascii->str
str->ascii
字符串到bcd是不能相互转换的,中间必须有转ascii的过程
即 bcd->ascii->str
str->ascii->bcd
而且你上面的那个测试数据里,好像G是不应该出现的吧?
wizardblue 2006-03-23
  • 打赏
  • 举报
回复
public final static char[] BToA = "0123456789abcdef".toCharArray() ;

public static String BCD2ASC(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);

for (int i = 0; i < bytes.length; i++) {
int h = ((bytes[i] & 0xf0) >>> 4);
int l = (bytes[i] & 0x0f);
temp.append(BToA[h]).append( BToA[l]);
}
return temp.toString() ;
}
uglyFairy 2006-03-23
  • 打赏
  • 举报
回复
TO :wizardblue(不死鱼)

处理结果:
原数据:1234567890
Bcd:[B@126b249
ASC:1234567890

原数据:ABG0123456789abcdef
Bcd:[B@182f0db
ASC:0ac00123456789abcdef

不知道是那里出错了,可以帮我看一下吗?
------------------------------------------

代码如下:
package Lib.AscBcd;

import java.nio.*;

/**
* @功能: ASC与BCD码的转化
* @生产者: 史建敏
* @生产时间: 2006-3-23
*/
public class AscBcd {

//
public final static char[] BToA = "0123456789abcdef".toCharArray() ;

/**
* @功能: 构造体
*/
public AscBcd() {
}

/**
* @param args
*/
public static void main(String[] args)
{
String sTestData = "1234567890";
System.out.println("原数据:" + sTestData);
System.out.println("Bcd:" + str2Bcd(sTestData));
System.out.println("ASC:" + bcd2Str(str2Bcd(sTestData)));

sTestData = "ABG0123456789abcdef";
System.out.println("原数据:" + sTestData);
System.out.println("Bcd:" + str2Bcd(sTestData));
System.out.println("ASC:" + bcd2Str(str2Bcd(sTestData)));
}

/**
* @函数功能: BCD码转为10进制串(阿拉伯数据)
* @输入参数: BCD码
* @输出结果: 10进制串
*/
public static String bcd2Str(byte[] bytes) {

StringBuffer temp = new StringBuffer(bytes.length * 2);

for (int i = 0; i < bytes.length; i++) {
int h = ((bytes[i] & 0xf0) >>> 4);
int l = (bytes[i] & 0x0f);
temp.append(BToA[h]).append( BToA[l]);
}
return temp.toString() ;
}

/**
* @函数功能: 10进制串转为BCD码
* @输入参数: 10进制串
* @输出结果: BCD码
*/
public static byte[] str2Bcd(String asc) {
// 原数据的长度
int len = asc.length();
int mod = len % 2;

if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}

// 原数据
byte bOriginalData[] = new byte[len];
if (len >= 2) {
len = len / 2;
}

// 转换后的BCD码
byte bBCD[] = new byte[len];

// 将字符串数据转换成字节数据
bOriginalData = asc.getBytes();

int j, k;
for (int p = 0; p < asc.length()/2; p++) {
if ( (bOriginalData[2 * p] >= '0') && (bOriginalData[2 * p] <= '9')) {
j = bOriginalData[2 * p] - '0';
} else if ( (bOriginalData[2 * p] >= 'a') && (bOriginalData[2 * p] <= 'z')) {
j = bOriginalData[2 * p] - 'a' + 0x0a;
} else {
j = bOriginalData[2 * p] - 'A' + 0x0a;
}

if ( (bOriginalData[2 * p + 1] >= '0') && (bOriginalData[2 * p + 1] <= '9')) {
k = bOriginalData[2 * p + 1] - '0';
} else if ( (bOriginalData[2 * p + 1] >= 'a') && (bOriginalData[2 * p + 1] <= 'z')) {
k = bOriginalData[2 * p + 1] - 'a' + 0x0a;
}else {
k = bOriginalData[2 * p + 1] - 'A' + 0x0a;
}

int a = (j << 4) + k;
byte b = (byte) a;
bBCD[p] = b;
}
return bBCD;
}
}
wizardblue 2006-03-23
  • 打赏
  • 举报
回复
你什么意思? public static String bcd2Str(String s)这样?
uglyFairy 2006-03-23
  • 打赏
  • 举报
回复
TO :wizardblue(不死鱼)
BCD2ASC 可以处理阿拉伯数字不能处理英文字母

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧