?
Solved

Java newbie Help needed

Posted on 2004-11-11
5
Medium Priority
?
206 Views
Last Modified: 2010-03-31
hai,

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,

Ashok.G
0
Comment
Question by:ashok3sep
  • 3
5 Comments
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 12556692
It needs to be:

sum += identify[i];

You need to index into the array.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 12557330
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);
0
 
LVL 5

Author Comment

by:ashok3sep
ID: 12583622
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12587420
>>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);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12587423
8-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question