Monday, 15 August 2011

Java string hex to int -



Java string hex to int -

i want convert below string hex int (16).

a6 5f de 57

what have done, don't know it's right way , can't verify if number right or not.

byte[] rec = new byte[20]; datagrampacket prec = new datagrampacket(rec, rec.length); socket.receive(prec); string tdata = ""; for(int = 0; < 20; i++){ tdata += string.format("%02x", rec[i]); }

the output packet:

ffffffff730aa65fde5700000000000000000000

and must skip first 6 bytes [ffffffff730a] , need remaining 4 bytes [a65fde57]. note: [00000000000000000000] null because length of buffer.

system.out.println( hextoint(tdata, 6, 10) ); private integer hextoint(string str, int start, int end){ string t = str.substring(start, end); char[] ch = t.tochararray(); string res = ""; for(int = 0; < end-start; += 2){ res += integer.parseint(ch[i]+ch[i+1]+"", 16); } homecoming integer.parseint(res); }

and result is:

516262

is right way convert hex string int? result correct?

simplest thing utilize int = integer.parseint(...). see integer.parseint(java.lang.string, int)

java string int hex

No comments:

Post a Comment