i want to write a code for big endian and little endian represntation of message.The msg is trading message.How can i do it with core java?

also how can i check data type validity of input given by user and data type is also provided by user.
Both Data type ind value i m asking to the user.& i wznt to check that whether user has entered correct value?

Recommended Answers

All 7 Replies

This will keep you very busy for a while........... :-) :)

1. Big endian machine: Stores data big-end first. When looking at multiple bytes, the first byte (lowest address) is the biggest.
Motorola processors are this kind hardware.
Unix
2. Little endian machine: Stores data little-end first. When looking at multiple bytes, the first byte is smallest.
Intel processors are this kind hardware.
Windowsx

Do you need to convert little-E to big-E ?
What is your message is made of text only or number & text ?


Albert

Look at the ByteBuffer class. You can wrap a ByteBuffer around some bytes that you need to decode, then set the byte order on the buffer to either little/big endian (or the machine's native type), then pull out ints etc.

Similarly, to ENCODE a value in little/big endian, allocate a ByteBuffer, set to appropriate endianness, store the value (with putInt() etc), then query the underlying array.

There's some information about ByteBuffer and using them with different byte orders on my web site that might be helpful (and of course take a look at the javadoc).

This will keep you very busy for a while........... :-) :)

1. Big endian machine: Stores data big-end first. When looking at multiple bytes, the first byte (lowest address) is the biggest.
Motorola processors are this kind hardware.
Unix
2. Little endian machine: Stores data little-end first. When looking at multiple bytes, the first byte is smallest.
Intel processors are this kind hardware.
Windowsx

Do you need to convert little-E to big-E ?
What is your message is made of text only or number & text ?


Albert

We want to compose message as follow.
for example...

symbol : 5 bytes : alphanumeric
action : 1 byte : alphanumeric
time : 7 bytes : alphanumeric
mc id : 1 bytes : alphanumeric
prize : 10 bytes :numeric

etc.

message can be composed of other fields having data types numeric,alphanumeric,binary integer values etc.

Look at the ByteBuffer class. You can wrap a ByteBuffer around some bytes that you need to decode, then set the byte order on the buffer to either little/big endian (or the machine's native type), then pull out ints etc.

Similarly, to ENCODE a value in little/big endian, allocate a ByteBuffer, set to appropriate endianness, store the value (with putInt() etc), then query the underlying array.

There's some information about ByteBuffer and using them with different byte orders on my web site that might be helpful (and of course take a look at the javadoc).

i have tried to import "nio" package in my program but it is giving error as follows:

chk.java:2: package java.nio does not exist
import java.nio.*;
^
1 error


I am using jdk1.4.2 edition?
Is there any solution.

java.io.*

No, it is java.nio for the ByteBuffer class. And if you're using JDK 1.4, it really should find it! Can't really suggest anything other than make sure your JDK is properly installed and that your IDE is properly set up ti use it.

Do you know what is total bytes of this message ?
Also you can read document about "DataInputStream" from java.io.* of jdk 1.4 or higher version will lead you to understand
what you need to do.

The following sample codes just gives you some ideas.
package com.my.Message;

import java.io.*;


public genMessage {
private ByteArrayOutputStream outputBuffer = null;
private DataOutputStream outputStream = null;
private DataInputStream inputStream = null;

genMessage()
   {

    outputBuffer = new ByteArrayOutputStream();
    outputStream = new DataOutputStream(outputBuffer);
  }

 public byte readByte(){
     try {
      return inputStream.readByte();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
 public int readInt(){
     try {
      return inputStream.readInt();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
 public String readUTF(){
     try {
       return inputStream.readUTF();
     }catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
  public void writeObj(Object value) {
     outputBuffer = new ByteArrayOutputStream();
     outputStream = new DataOutputStream(outputBuffer);
     try {
            if(value instanceof String )
                outputStream.writeUTF((String) value);
           else if (value instanceof  Integer )
                   outputStream.writeInt(((Integer) value).intValue()); 
           else if ..............

       }catch (IOException ie) {
         System.out.println("Exception: " + ie);
     }
  }

Albert

commented: Still refuses to use code tags. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.