在以下函数中,我遇到了 Array out of Bounds 问题。它应该将一串数字转换为 BCD 格式,如下所示: "12345" -> 0x01 0x23 0x45 。字符串的长度未知。
public void StringtoBCD(String StringElement)
{
ByteArrayOutputStream in = new ByteArrayOutputStream();
if (!" ".equals(StringElement)){
int i=0;
byte[] tempBCD = StringElement.getBytes();
for (i=0; i<tempBCD.length; i++){
tempBCD[i]=(byte)(tempBCD[i]-0x30);
}
i=0;
if (tempBCD.length %2 !=0){
in.write(0);
}
while(i<tempBCD.length){
in.write((tempBCD[i]<<4)+tempBCD[i+1]);
i=i+2;
}
}
}
我尝试了类似的东西
while(i<tempBCD.length){
in.write((tempBCD[i]<<4)+tempBCD[i+1]);
if (i+3>(tempBCD.length)){
i+= 1;
}
else {
i+=2;
}
}
没有成功。我很确定这很简单,但似乎我在这里监督了一些事情。任何帮助表示赞赏:)