[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Java Basics Help

Posted on 2004-11-15
Medium Priority
211 Views
if i have a following code

int sum = 433;
byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (byte)(sum % 256));

when i print it i have a negative value for the lower order byte

but i like to have a positive value
how could i acheive it

with regards,

Ashok.G
0
Question by:ashok3sep

LVL 5

Author Comment

ID: 12583718
I have a value for Higher order byte as 1

value for Lower order byte = -79

but i dont want to have -79

but i would like to have a remainder of the sum%256

how can i do it
0

LVL 37

Expert Comment

ID: 12583787
System.out.println("Lower order byte = " + (int)(sum % 256));

prints: 177
0

LVL 85

Expert Comment

ID: 12583792
(sum%256+256)%256
0

LVL 37

Expert Comment

ID: 12583808
The max value a byte can have is 2^7-1 = 127
0

LVL 5

Author Comment

ID: 12583819
but i want to add in to a byte array checksum
0

LVL 37

Expert Comment

ID: 12583844
Since,
>>The max value a byte can have is 2^7-1 = 127
The min value a byte can have is -2^7 = -128
it will be difficult to add 177 in that array (while -97 is possible)
0

LVL 37

Expert Comment

ID: 12583913
This is how you get your original number back:

int sum = 433;
byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (int)(sum % 256));
int result = (int) (256*checksum[0]);
result += (int)(256+checksum[1]);
System.out.println( result );
0

LVL 5

Author Comment

ID: 12584109
I need to some how add the byte checksum with no negative values to the array temp3.

how could i acheive it.

byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256 ) };
byte[] temp3 = new byte[ identify.length + checksum.length] ;

System.arraycopy( identify, 0, temp3, 0, identify.length ) ;
System.arraycopy(checksum, 0, temp3, identify.length, checksum.length ) ;
identify = temp3 ;

for (int i = 0; i < identify.length; i++)
{
System.out.println("Elements at " + i + " = " + identify[i]);
}

With regards,

Ashok.G
0

LVL 37

Expert Comment

ID: 12584139
Did you missed my previous post?

>>The max value a byte can have is 2^7-1 = 127
>>The min value a byte can have is -2^7 = -128
So, it is *impossible* to add the value 177 into the byte array!
0

LVL 21

Expert Comment

ID: 12587001
You can also use bit masking to get the low/hi order bytes:
byte[] checksum = {(byte)(sum & 0x00FF) ,(byte)((sum & 0xFF00) >>>8) };
(the >>> is an unsigned right shift)

The two values will be positive values as long as the sum wasn't greater than 2^15-1 (32767).
0

LVL 37

Expert Comment

ID: 12591324
>> The two values will be positive values as long as the sum wasn't greater than 2^15-1 (32767).
Sure of that?

sum = 433;      in hex: 01B1                   8 7 6 5  4 3 2 1    8 7 6 5  4 3 2 1
0 0 0 0  0 0 0 1    1 0 1 1  0 0 0 1

bitwise & with    0x00FF                         0  0 0 0  0 0 0 0   1 1 1 1  1 1 1 1   = 0x00B1 = 177 decimal

How would you put 177 in a byte?

checksum[0] = -79
0

LVL 37

Accepted Solution

zzynx earned 100 total points
ID: 12591392
ashok3sep, a correction:

int sum = 433; // or 233 to test the "else" path of the added if statement

byte[] checksum = {(byte)(sum / 256) ,(byte)(sum % 256) };

System.out.println("Higher order byte = " + (byte)(sum / 256));
System.out.println("Lower order byte = " + (int)(sum % 256));
int result = (int) (256*checksum[0]);
if (checksum[1]<0)                                  // <<<<<< condition added
result += (int)(256+checksum[1]);
else
result += (int)(checksum[1]);
System.out.println( result );
0

LVL 37

Expert Comment

ID: 12593105
Hi ashok, any news?
0

LVL 37

Expert Comment

ID: 12608169
Thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a clâ€¦
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correctâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
###### Suggested Courses
Course of the Month18 days, 6 hours left to enroll