harmeek_80
asked on
Getting bytes from the bytes array
Hi Friends,
I receive data from the server and I store it in a bytes array.Now I need to retrieve bytes from the array,like first byte says code[1 byte],second says lastname[4 bytes],third says fname[30 bytes],last byte says if there is one more entry,and the same syntax needs to be followed again....I tried this but its not working...any help would be appreciated.
import java.io.*;
import java.nio.*;
public class btest{
public static void main(String[] ar){
try{
String data1 ="1232 3 3 444 222 111 444 555555 2222222 1111111 3333 dssssddd";
byte[] data = data1.getBytes("ASCII");
int length = data.length;
System.out.println("length is:"+length);
ByteBuffer buff = ByteBuffer.wrap(data);
byte[] fbyte = new byte[1];
byte[] lname = new byte[4];
byte[] fname = new byte[30];
byte[] lbyte = new byte[1];
buff.get(fbyte, 0,1);
String received0 = new String(fbyte,"ASCII");
System.out.println("Data Recd: " + received0);
buff.position(1);
buff.get(ref_no, 1,4);
String received1 = new String(lname,"ASCII");
buff.position(4);
buff.get(uname, 4,34);
String received2 = new String(fname,"ASCII");
System.out.println("Data Recd: " + received2);
buff.position(34);
buff.get(lbyte, 34,35);
String received3 = new String(lbyte,"ASCII");
System.out.println("Data Recd: " + received3);
}catch(Exception e){e.printStackTrace();}
}
}
I receive data from the server and I store it in a bytes array.Now I need to retrieve bytes from the array,like first byte says code[1 byte],second says lastname[4 bytes],third says fname[30 bytes],last byte says if there is one more entry,and the same syntax needs to be followed again....I tried this but its not working...any help would be appreciated.
import java.io.*;
import java.nio.*;
public class btest{
public static void main(String[] ar){
try{
String data1 ="1232 3 3 444 222 111 444 555555 2222222 1111111 3333 dssssddd";
byte[] data = data1.getBytes("ASCII");
int length = data.length;
System.out.println("length
ByteBuffer buff = ByteBuffer.wrap(data);
byte[] fbyte = new byte[1];
byte[] lname = new byte[4];
byte[] fname = new byte[30];
byte[] lbyte = new byte[1];
buff.get(fbyte, 0,1);
String received0 = new String(fbyte,"ASCII");
System.out.println("Data Recd: " + received0);
buff.position(1);
buff.get(ref_no, 1,4);
String received1 = new String(lname,"ASCII");
buff.position(4);
buff.get(uname, 4,34);
String received2 = new String(fname,"ASCII");
System.out.println("Data Recd: " + received2);
buff.position(34);
buff.get(lbyte, 34,35);
String received3 = new String(lbyte,"ASCII");
System.out.println("Data Recd: " + received3);
}catch(Exception e){e.printStackTrace();}
}
}
> buff.get(ref_no, 1,4);
your problem is that those values refer to where in ref_no to place the bytes :)
What you want can also easily be done without even needing to create a ByteBuffer, and use Syste.arraycopy to copy the bytes directly from the byte array
your problem is that those values refer to where in ref_no to place the bytes :)
What you want can also easily be done without even needing to create a ByteBuffer, and use Syste.arraycopy to copy the bytes directly from the byte array
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not sure how that answer added anything to the one i'd already given, although it did repeat it
buff.get(fbyte);
String received0 = new String(fbyte,"ASCII");
System.out.println("Data Recd: " + received0);
buff.get(ref_no);
etc.