如何将字符串替换为android

时间:2017-08-15 00:18:45

标签: java android

如何在android中替换一个Byte数组项 我的代码是

public  void saldohext(View view){
    EditText tvBalanceBIP = (EditText)findViewById(R.id.txtHexa);
    int balance = Integer.parseInt(tvBalanceBIP .getText().toString()) * 100;
    int total = 65535;
    int diference = total - balance ;

    String StrBalance = Integer.toHexString(balance ).toString();
    String StrDiference  = Integer.toHexString(diference ).toString();

    byte a = Byte.decode("0x"+StrBalance .substring(2));
    byte b = Byte.decode("0x"+StrBalance .substring(0, 2));
    byte c = Byte.decode("0x"+StrDiference  .substring(2));
    byte d = Byte.decode("0x"+StrDiferencia.substring(0,2));

    DATA_CARGA = new byte[]{(byte) a, (byte) b,(byte)0x00,(byte)0x00,
                            (byte) c,(byte) d,(byte)0xff,(byte)0xff,
                            (byte) a,(byte) b,(byte)0x00,(byte)0x00,
                            (byte)0x00,(byte)0x02,(byte)0x01,(byte)0x86};
    tvSaldoBIP.setText(DATA_CARGA.toString());
}

我的设备显示

  

“值250输出范围从输入0xfa”

1 个答案:

答案 0 :(得分:0)

您的“超出范围”问题是尝试Byte.decode超出C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64数据类型范围的值,这将导致它抛出byte

来自Java的Primitive Data Types文档:

  

<强>字节
  字节数据类型是8位带符号的二进制补码整数。它的最小值为-128,最大值为127   (含)。

现在,让我们通过调试器运行代码来举例:

NumberFormatException

如评论中所示,当我尝试 // I've hardcoded balance here as an example. int balance = 55 * 100; int total = 65535; int diference = total - balance; // You don't need to call toString() here because // toHexString()'s output is already a String. String StrBalance = Integer.toHexString(balance); //"157c" String StrDiference = Integer.toHexString(diference); //"ea83" String sub; byte a = Byte.decode("0x"+StrBalance.substring(2)); //OK, 0x7c=124 byte b = Byte.decode("0x"+StrBalance.substring(0,2)); //OK, 0x15=21 sub = StrDiference.substring(2); //"83" byte c; try { c = Byte.decode("0x"+sub); //NG, because 0x83=131 > 127 } catch (NumberFormatException e) { c = 0; //Byte.decode will throw a NumberFormatException } “0x83”时,它将失败,因为0x83 = 131,大于Byte.decode的最大值127(0x7F)。如果没有byte块,那就会产生这个未处理的异常:

  

引起:java.lang.NumberFormatException:超出范围的值   字节:“0x83”

与你得到的相同(你试图try-catch“0xfa”)。

要解决此问题,请尝试使用Java的ByteBuffer类,它将在内部为您处理转换,并允许您将Byte.decode直接转换为int数组,跳过所有byte次转化,因为在您的方法结束时,您需要的是String的字节。

DATA_CARGA

您甚至可以通过将 int balance = Integer.parseInt(tvBalanceBIP .getText().toString()) * 100; int total = 65535; int diference = total - balance; byte[] balanceBytes = ByteBuffer.allocate(4).putInt(balance).array(); balanceBytes[0] = (byte)0x00; balanceBytes[1] = (byte)0x00; byte[] diferenceBytes = ByteBuffer.allocate(4).putInt(diference).array(); diferenceBytes[0] = (byte)0xFF; diferenceBytes[1] = (byte)0xFF; DATA_CARGA = new byte[] { balanceBytes[3], balanceBytes[2], balanceBytes[1], balanceBytes[0], diferenceBytes[3], diferenceBytes[2], diferenceBytes[1], diferenceBytes[0], balanceBytes[3], balanceBytes[2], balanceBytes[1], balanceBytes[0], (byte)0x00, (byte)0x02, (byte)0x01, (byte)0x86 }; tvSaldoBIP.setText(DATA_CARGA.toString()); 声明为new byte[]然后使用可用的DATA_CARGA方法来构建字节数组(生成代码)来删除ByteBuffer代码清洁器)。