Results 1 to 3 of 3
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default Compile Error: Cannot cast from integer to byte

    I have a msg object that contains an ArrayList<Integer> collection. However, in order to send the elements in the array over the udp socket, it needs to be sent as a byte[] array. So why am I using ArrayList<Integer> over byte array in first place? Well when I receive data from socket from embedded c program, I need to get an unsigned representation of the data, and thus I need to store it in integers, since bytes in Java are unsigned and unsigned chars in c that are greater than 127 will yield incorrect values in java. But when I send an ack back over the socket, I need to send the data back as bytes. So I convert the ArrayList<Integer> to a byte array:

    Java Code:
    	byte[] data = msg.toByteArray();		
        	DatagramPacket response = new DatagramPacket(data, data.length,
                    packet.getAddress(), packet.getPort());
    
            public class Gprs {
              ...
    	  public byte[] toByteArray(){
    		int i=0;
    		byte[] data = new byte[m_data.size()];
    		for(Integer item : m_data){
    			data[i] = (byte)m_data.get(i);
    			++i;
    		}
    	  }

    The problem is I get an "Cannot cast from Integer to byte" when trying to cast the integer to byte: data[i] = (byte)m_data.get(i);

    How can I resolve this?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Compile Error: Cannot cast from integer to byte

    Cannot cast from Integer to byte
    Integer is an object, byte is a primitive. An object can't be cast to a primitive.
    Try getting the int value out of the Integer object and cast that.
    Last edited by Norm; 05-29-2014 at 12:22 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mousumiy is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Compile Error: Cannot cast from integer to byte

    Although the concept of casting is reasonably simple, the usage is complicated by the fact that Java has both primitive types (such as int, float, and boolean) and object types (String, Point, ZipFile, and the like).

    So what you need to do is to first get the inetrger value of the Object and use that to cast.

Similar Threads

  1. Replies: 0
    Last Post: 06-10-2013, 10:48 AM
  2. cast double to byte
    By karang in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 09:08 AM
  3. Cast error
    By nat45928 in forum New To Java
    Replies: 7
    Last Post: 04-26-2011, 02:48 AM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 01:04 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •