Writing a short to binary .dat file as Little-Endian

Currently, I have a code that's writing type short data to a .dat file using DataOutputStream.  From what I understand, Java sends out the bytes as BigEndian, but the output to the file needs be send out as LittleEndian (Intel format) to be able to be read by a software that expects the Intel format.

For example, sending a type short value of 583 appears in TextPad as 0247 (big-endian hex), but I should be seeing 4702 (little-endian hex).

I searched for a solution online and found out about
    ByteBuffer. order( ByteOrder.LITTLE_ENDIAN )

I'm not sure how I'm suppose to use this with the example snippet code below or if there are other ways of doing this.
int data = 0;
data = Integer.parseInt(stringData);
outData=(short)data;
os.writeShort(outData);

Open in new window

TuringMachine0101Asked:
Who is Participating?
 
Kevin CrossChief Technology OfficerCommented:
0
 
Kevin CrossChief Technology OfficerCommented:
Here is the code example in your specific case:
ByteBuffer bb = ByteBuffer.allocate(Short.SIZE/8);
bb.putShort(outData);
bb.order(ByteOrder.LITTLE_ENDIAN);
os.writeShort(bb.getShort(0));

Open in new window

0
 
TuringMachine0101Author Commented:
Guys/gals, I greaty appreciate the assistance given.
This worked well for some cases in my code.

For example,  when using a switch function, as in the code below, it worked well each time.
Inside the for loop code, it doesn't seem to work since the values don't appear to be sent out as little-endian.

For example, when I look at short type value 3995 in Textpad, it gives me 0F 9B instead of little-endian 9B 0F.  Likewise, 344 shows as 01 58 instead of 58 01.

I don't know if there is something I have to do to handle multipe uses of the ByteBuffer.

ByteBuffer bb = ByteBuffer.allocate(Short.SIZE/8);
 
try{
  switch(sw){
    case 1: bb.putShort(swSF1); b.order(ByteOrder.LITTLE_ENDIAN);  
            os.writeShort(bb.getShort(0)); bb.clear(); break;
    case 2: bb.putShort(swSF2); bb.order(ByteOrder.LITTLE_ENDIAN);  
            os.writeShort(bb.getShort(0)); bb.clear(); break;
    case 3: bb.putShort(swSF3); bb.order(ByteOrder.LITTLE_ENDIAN);
            os.writeShort(bb.getShort(0)); bb.clear(); break;
    case 4: bb.putShort(swSF4); bb.order(ByteOrder.LITTLE_ENDIAN);
            os.writeShort(bb.getShort(0)); bb.clear(); break;
    default: os.writeShort(blank); break;
  }
      .
      .
      .
  for(int i=2; i<sl; i++){
        .
        .
        .
    if(value >= 0){
      if(value > 4096){
        msb = (short)(value / 4096);
        lsb = (short)(value % 4096);
                                        
        bb.putShort(msb);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        os.writeShort(bb.getShort(0));
        bb.clear();
 
        bb.putShort(lsb);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        os.writeShort(bb.getShort(0));
        bb.clear();
      }
      else{
        msb = 0;
        lsb = (short)value;
 
        bb.putShort(msb);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        os.writeShort(bb.getShort(0));
        bb.clear();
 
        bb.putShort(lsb);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        os.writeShort(bb.getShort(0));
        bb.clear();
      }
    }
  }
}catch{...}

Open in new window

0
 
TuringMachine0101Author Commented:
I appreciate the assistance.  I added another question (debugging perhaps?) on top of my original after I had tried the suggestion.  If I need to, I could open up another question for debugging it.  But in regards to the original, what is the division by eight in ByteBuffer.allocate(Short.SIZE/8) mean?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.