java - How remplace string to a byte in android -


how can remplace 1 item of byte array in android
my code it

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 = 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()); } 

and divice show me "value 250 out range input 0xfa"

your "out of range" problem trying byte.decode value outside range of byte data type, cause throw numberformatexception.

from java's primitive data types docs:

byte:
byte data type 8-bit signed two's complement integer. has minimum value of -128 , maximum value of 127 (inclusive).

now, let's take example code running through debugger:

    // i've hardcoded balance here example.     int balance = 55 * 100;      int total = 65535;     int diference = total - balance;      // don't need call tostring() here because     // tohexstring()'s output string.     string strbalance = integer.tohexstring(balance);      //"157c"     string strdiference  = integer.tohexstring(diference); //"ea83"      string sub;     byte = 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 throw numberformatexception     } 

as shown in comments, when try byte.decode "0x83", fail because 0x83=131 greater byte's max value of 127 (0x7f). without try-catch block, that's going produce unhandled exception:

caused by: java.lang.numberformatexception: value out of range byte: "0x83"

which same got (you trying byte.decode "0xfa").


to solve this, try using java's bytebuffer class instead, internally handle conversions , allow convert int's directly byte arrays, skipping string conversions since, @ end of method, need bytes 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()); 

you can rid of new byte[] code declaring data_carga bytebuffer using available put(..) methods build byte array (which makes code cleaner).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -