now the problem in my code is only that compiler is not taking the same value as given in the array. I wanted to take values like 58, 80, D, 1, 4 and checksum of the same. so now please help me and tell me how can I obtain the above values from the values given in my code in the array.
i.e how can i make my byte array to be appear like this for the compile so it can give the correct output
for(int i = 0; i < size ; i++)
String str = scanner.next();
int x = Integer.parseInt(str,16);
String hexString = Integer.toHexString(x);
int y = Integer.parseInt(hexString,16);
byteArray[i] = (byte)y;
Your code looks a bit weird. You're mixing up values and representations of the values.
At this point you've got your correct int value. F.e. you typed 17 (of which you say it's a hex number). This int value now contains the value 23. Which is correct.
You can now store this value in your bytearray. And when you want to print it, you can use my code in order to represent it in a hex format.
duhit Choudhary wrote:i.e how can i make my byte array to be appear like this for the compile so it can give the correct output...
Well, what you wrote as output is likely to be very confusing. If someone sees the string "(byte) 58"
without any indication of what they're looking at, the chances are that they will assume (incorrectly) that it's a decimal number. That's whyJava uses the '0x' notation.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here