字节流问题?:BCD码,按位操作及其它

meeting 2003-10-17 10:16:30
如下问题有请各路高手帮忙:

1.字节数值: 数据由4个字节组成,排列由低位字节到高位字节,表示一个16进制数值 (还有可能是2字节,8字节表示)

2.BCD数值:数据由多个字节组成,排列由高到低,每个字节内由两个BCD码组成,高4位表示前一个数字,低4位表示后一个数字,最后一个字节不够两个数字时,低4位补0XF,小数点用0XA,”-”号用0XB, 数值表示遵循10进制数值表示法格式

要处理 字节数组<===>数值 之间的相互转换
记得是用JAVA语言
...全文
86 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
meeting 2003-10-20
  • 打赏
  • 举报
回复
初步搞定,熟悉的帮忙看看是否正确
//1.-----------------------------------------------------------------------
public static byte[] hex2Bytes(int hex, int size) {
byte[] result = new byte[size];
for(int i=0;i<size;i++){
result[i] = (byte) ( ( ( (long) hex) >> (8 * i)) & 0xFF);
}
return result;
}
public static int byte2Hex(byte[] packetBytes) {
int result = 0;
if (packetBytes == null) {
return result;
}
for (int i = 0; i < packetBytes.length; i++) {
result += (packetBytes[i] & 0xff) << (i * 8);
}
return result;
}
//2.-----------------------------------------------------------------------
public static byte[] double2BCD(double d) {
byte[] byteReturn = null;
int cycle = 0;
boolean needSuffix = false;
String s = Double.toString(d);
int len = s.length();
if (len % 2 == 0) {
needSuffix = false;
len = len / 2;
cycle = len;
}
else {
needSuffix = true;
len = (len + 1) / 2;
cycle = len - 1;
}
byteReturn = new byte[len];
int int1 = 0, int2 = 0;

for (int i = 0; i < cycle; i++) {
String temp1 = s.substring(2 * i, 2 * i + 1);
String temp2 = s.substring(2 * i + 1, 2 * i + 2);
if (temp1.equals("-")) {
int1 = 0xB;
}
else if (temp1.equals(".")) {
int1 = 0xA;
}
else {
int1 = Integer.parseInt(temp1);
}
int1 = int1 * 16;

if (temp2.equals("-")) {
int2 = 0xB;
}
else if (temp2.equals(".")) {
int2 = 0xA;
}
else {
int2 = Integer.parseInt(temp2);
}
byteReturn[i] = (byte) ( (int1 + int2) & 0xFF);
}
//补位0xF
if (needSuffix) {
int suffix = Integer.parseInt(s.substring(2 * len - 2, 2 * len - 1));
byteReturn[len - 1] = (byte) (suffix * 16 + 0xF);
}
return byteReturn;
}

public static double byteBCD2Double(byte[] packetBytes) {
if (packetBytes == null) {
return 0;
}
int length = packetBytes.length;
byte b = 0;
String stringValue = "";
boolean hasMinus = false, hasDot = false;
for (int i = 0; i < length; i++) {
String temp = Integer.toHexString(packetBytes[i] & 0xFF);
if(temp!=null && temp.length()<2){
temp="0"+temp;
}
stringValue = stringValue + temp;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < stringValue.length(); i++) {
char c = stringValue.toUpperCase().charAt(i);
if (c == 'B') {
c = '-';
sb.append(c);
}
else if (c == 'A') {
c = '.';
sb.append(c);
}
else if (c != 'F') {
sb.append(c);
}
}
stringValue = sb.toString();
return Double.valueOf(stringValue).doubleValue();
}
whywzf 2003-10-17
  • 打赏
  • 举报
回复
1111(高4位)
对多字节,不一定[0]是高位,还是[3]是高位
在我写的方法中,是定义【0】是低字节的,比如i=1,那么[01][00][00][00],所以说是由低到高的,根据开发者的习惯或者某些性能上可以造成便利的原因,低--->高;高--->低两种情况都存在。比如汉字的双字节是高到低的,音频的是低到高的;支持intelCPU的是低到高的,其他CPU也有高到低的……
meeting 2003-10-17
  • 打赏
  • 举报
回复
近来我脑子总是不灵通,很小的问题都混淆了,
笨笨的问一下whywzf(古风) :
关于高字节低字节,字节的高四位及低四位的概念常混淆,请解答一下

对一个字节: 1111 0000 那里是高四位?
对多个字节如: [0][1][2][3]究竟是[0]还是[3]是高位字节?
whywzf 2003-10-17
  • 打赏
  • 举报
回复
我写的就是由低到高
问题2考虑中……
meeting 2003-10-17
  • 打赏
  • 举报
回复
关于2:
比如:十进制数 -325.25
变为字节数组就是:
[-3][25][.2][5 ]

[B3][25][A2][5F]

所占的字节数由具体的数字应该占多少就多少
meeting 2003-10-17
  • 打赏
  • 举报
回复
上面我写的回复一个打错"=int2Bytes(0x16)即同 int2Bytes(32);"
应该为"=int2Bytes(0x16)即同 int2Bytes(22);"

其实1的问题很易搞掂,不过就是概念"排列由低位字节到高位字节"不太明,
是否如: whywzf(古风) 说的还是把其反过来?

对于: xwlovesh(xiewei) 的提问那个数值分别是1与2的

请大侠继续搞掂2, thanks
xwlovesh 2003-10-17
  • 打赏
  • 举报
回复
在“字节数组<===>数值”中的数值到底是(由1.字节数值: 数据由4个字节组成),
还是由(2.BCD数值:数据由多个字节组成)?
whywzf 2003-10-17
  • 打赏
  • 举报
回复
//4字节,2和8的自己搞定,同理
public static byte[] IntToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte)(i & 0xFF);
result[1] = (byte)((i >> 8) & 0xFF);
result[2] = (byte)((i >> 16) & 0xFF);
result[3] = (byte)((i >> 24) & 0xFF);
return result;
}
meeting 2003-10-17
  • 打赏
  • 举报
回复
另外第1题本来就是int,进制不同不影响,方法大概是 byte[] int2Bytes(int hex){}
调用时 =int2Bytes(0x16)即同 int2Bytes(32);
meeting 2003-10-17
  • 打赏
  • 举报
回复
我能看懂代码,只要结果,大侠们帮帮忙
BCD码指8421码
whywzf 2003-10-17
  • 打赏
  • 举报
回复
考虑是不是可以先转成int(或其他),再转BCD?

62,614

社区成员

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

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