Trying to create a method to format HEX strings in Android Java -


i trying create method formats hex string , breaks string each 2 characters equals 1 byte. want have byte array of three, e.g. string "012254" "01", "22", "54". have created methods, getting 'numberformatexception' saying invalid int: "". please @ code , @ logcat , see can fix problem. thank in advance.

here code:

/*  * converts set of hex digits array of 8-bit binary bytes.  * stops @ first non-hex character if before end of string.  * hex bytes assumed pairs of digits i.e. least significant byte (lsb) -> significant byte (msb) significant nibble first.  * e.g. string: "f20c345a" convert values f2,0c,34,5a.  * adds zeroes end of string make last digits length byte.  */ public byte[] tobytearray(string hexstring) {        int offset = 0;     int hexlength = 0;     int bytes = 0;     string bytestring = "";     byte[] bytearray;      hexlength = hexsequencelength(hexstring); // check hex sequence.     hexstring = hexstring.substring(0, hexlength); // ignore after hex sequence.      bytes = hexlength / charsperbyte; // calculate how many words represented string.      int rem = hexlength % charsperbyte;      if(rem != 0)     {         /*          * hex string short.          * add padding zeroes fill boundary of complete word.          */         int paddingzeroes = charsperbyte - rem;          if((paddingzeroes & (int)0x01) != 0)         {             /*              * odd number of padding zeroes.              * means first 0 must inserted before last digit keep least significant nibble (4-bit).              */             hexstring = new stringbuilder(hexstring).insert(hexlength-1, "0").tostring();              paddingzeroes--;         }          /* add zeroes end of string msb's of last value zero. */         for(int i=0; i<paddingzeroes; i++)         {             hexstring += "0";         }          /* recalculate length. */         hexlength = hexsequencelength(hexstring);         bytes = hexlength / charsperbyte;     }      if(bytes > 0)     {         bytearray = new byte[bytes]; // set array receive these values.          for(int i=0; i<bytes; i++)         {             bytestring = hexsubstring(hexstring, offset, charsperbyte, false); // isolate digits single byte.              bytearray[i] = (byte)integer.parseint(bytestring, 16); // parse value binary data array.              offset += charsperbyte; // set next word hex.             }          byte subsystem = bytearray[0];         byte highlevel = bytearray[1];         byte lowlevel = bytearray[2];         string bytesvalue = new string(bytearray);          system.out.println("byte array size: " + bytearray.length);         system.out.println("byte 1: " + subsystem);         system.out.println("byte 2: " + highlevel);         system.out.println("byte 3: " + lowlevel);         system.out.println("byte array value: " + bytesvalue);         // todo: need actual value of bytes, result returns 16.     }     else     {         bytearray = new byte[0]; // no hex data.          //throw new hexexception();     }      return bytearray; }  /*  * isolates hex value sub-string given hex character string start index , character length.  * includes option re-order digit pairs change order of significance of bytes in hex number.  */ private string hexsubstring(string src, int index, int length, boolean reverse) {     int hexnumbers = length / charsperbyte;     string[] hexnumber = new string[hexnumbers];     int numberindex = index;     string hexsubstring = "";      for(int i=0; i<hexnumbers; i++)     {            hexnumber[i] = src.substring(numberindex, charsperbyte+2);         numberindex += charsperbyte;          if(numberindex > src.length())         {             break;         }     }      if(reverse == false)     {         for(int i=0; i<hexnumbers; i++)         {             hexsubstring += hexnumber[i];         }     }     else     {         // reverse order make msb -> lsb.         for(int i=hexnumbers; i>0; i--)         {             hexsubstring += hexnumber[i-1];         }     }      return hexsubstring; }  /*  * gets number of consecutive hex characters start of string.  */ private int hexsequencelength(string hexstring) {     int length = 0;      char[] chararray = hexstring.tochararray();      for(char c : chararray)     {         if(hexchecker(c))         {             length++;         }         else         {             break;         }        }      return length; }  /*  * checks if character valid against hex values.  */ private boolean hexchecker(char c) {     string hexvalues = "0123456789abcdefabcdef";      if(hexvalues.indexof(c) > -1 == true)     {         system.out.println("it worked!");     }     else     {         system.out.println("it did not work!");     }      return hexvalues.indexof(c) > -1; } 

and here logcat:

10-07 11:53:11.420: e/androidruntime(4440): fatal exception: main 10-07 11:53:11.420: e/androidruntime(4440): java.lang.runtimeexception: unable start activity componentinfo{com.example.mobileapplicationretry/com.example.mobileapplicationretry.mainactivity}: java.lang.numberformatexception: invalid int: "" 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread.access$600(activitythread.java:141) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.os.handler.dispatchmessage(handler.java:99) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.os.looper.loop(looper.java:137) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread.main(activitythread.java:5103) 10-07 11:53:11.420: e/androidruntime(4440):     @ java.lang.reflect.method.invokenative(native method) 10-07 11:53:11.420: e/androidruntime(4440):     @ java.lang.reflect.method.invoke(method.java:525) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 10-07 11:53:11.420: e/androidruntime(4440):     @ dalvik.system.nativestart.main(native method) 10-07 11:53:11.420: e/androidruntime(4440): caused by: java.lang.numberformatexception: invalid int: "" 10-07 11:53:11.420: e/androidruntime(4440):     @ java.lang.integer.invalidint(integer.java:138) 10-07 11:53:11.420: e/androidruntime(4440):     @ java.lang.integer.parseint(integer.java:359) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.example.mobileapplicationretry.devicedescription.hexfunctions.tobytearray(hexfunctions.java:73) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.example.mobileapplicationretry.devicedescription.devicevalues.setupvalues(devicevalues.java:76) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.example.mobileapplicationretry.devicedescription.devicedescriptiondataset.setupdataset(devicedescriptiondataset.java:239) 10-07 11:53:11.420: e/androidruntime(4440):     @ com.example.mobileapplicationretry.mainactivity.oncreate(mainactivity.java:59) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activity.performcreate(activity.java:5133) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 10-07 11:53:11.420: e/androidruntime(4440):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 10-07 11:53:11.420: e/androidruntime(4440):     ... 11 more 

why not use existing classes this?

string myhexstr = "ab1257"; int hexint = integer.parseint(myhexstr, 16); bytebuffer buffer = bytebuffer.allocate(4); buffer.putint(hexint); byte[] result = buffer.array(); 

if want create array of 3, copy result array new smaller array. thing you'll need figure out byte want discard: 1st byte (msb), or last (lsb):

byte[] threebytearray = new byte[3];  int start = usemsb ? 0 : 1; (int = start; < threebytearray.length; i++)     threebytearray[i - start] = result[i]; 

hope helps :)


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -