Java newbie Help needed


the last part of the code gives me a error stating could not add int to a byte array.
how could i overcome this problem

byte[] identify = {0,10,0,20,2,0};
String PR_SName = "PRS2.0";

// get the bytes for the string to be appended
byte[] prs = PR_SName.getBytes("UTF-8") ;

// create a temp array the length we will need for the two arrays joined
byte[] tmp = new byte[ identify.length + prs.length ] ;

// copy the original array into the start of our temp array
System.arraycopy( identify, 0, tmp, 0, identify.length ) ;

// copy the appended array into the end of our temp array
System.arraycopy( prs, 0, tmp, identify.length, prs.length ) ;

// set the identify array to point to our newly created temp array
identify = tmp ;

// calculating the check summ of all the values in the array identify

int sum = 0;
for(int i = 0; i< identify.length;i++)
sum += identify;
System.out.println("Sum of all the bytes =" + sum);

// I need to apeend this check sum to the end of the Identify byte[] array

byte[] checktmp = {sum / 256,sum%256};

With regards,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It needs to be:

sum += identify[i];

You need to index into the array.
If you need to append an array two bytes long, then you need to

a. Size the tmp array to +2 bytes long
b. Use the following code

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

or more easily:

identity[identity.length - 2] = (byte)(sum / 256);
identity[identity.length - 1] = (byte)(sum % 256);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ashok3sepAuthor Commented:
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,

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

byte is a signed value, so you must 'unsign' it:

System.out.println("Lower order byte = " + ((byte)(sum % 256)) & 0xFF);

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.