Java newbie Help needed

Posted on 2004-11-11
Last Modified: 2010-03-31

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,

Question by:ashok3sep
    LVL 5

    Expert Comment

    It needs to be:

    sum += identify[i];

    You need to index into the array.
    LVL 86

    Accepted Solution

    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);
    LVL 5

    Author Comment

    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,

    LVL 86

    Expert Comment

    >>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);

    LVL 86

    Expert Comment


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    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…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    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:

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now