Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting bytes from the bytes array

Posted on 2006-10-21
5
204 Views
Last Modified: 2010-03-31
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();}
 
}
}

0
Comment
Question by:harmeek_80
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17782874
You don't need to move anything or calculation positions - the BB will do that for you

buff.get(fbyte);
String received0 = new String(fbyte,"ASCII");
System.out.println("Data Recd: " + received0);

buff.get(ref_no);


etc.
0
 
LVL 92

Expert Comment

by:objects
ID: 17782939
>           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
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 17782950
It's actually easier to do it with a BB - it's been designed especially for this sort of thing
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 17782969
you also need to remove those calls to position (they are relative gets) and do it in a loop
there are a variety of other minor prob also with the code
try something like:

boolean done = false;
while (!done) {
          buff.get(fbyte);
          String received0 = new String(fbyte,"ASCII");
          System.out.println("Data Recd: " + received0);
         
          buff.get(fname);
          String received1 = new String(fname,"ASCII");
          System.out.println("Data Recd: " + recieved1);
 
          buff.get(lname);
          String received2 = new String(lname,"ASCII");
          System.out.println("Data Recd: " + recieved2);

          buff.get(lbyte);
          String received3 = new String(lbyte,"ASCII");
          System.out.println("Data Recd: " + received3);
          done = !recieved3.equals(MORE_DATA);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17784478
Not sure how that answer added anything to the one i'd already given, although it did repeat it
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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
jdbc error in jsp application 20 63
Chrome and Firefox Java 5 50
Crystal Reports Licensing Questions 4 34
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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