?
Solved

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

Posted on 2009-04-23
4
Medium Priority
?
328 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:TuringMachine0101
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1500 total points
ID: 24219478
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1500 total points
ID: 24219548
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
 

Author Comment

by:TuringMachine0101
ID: 24226869
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
 

Author Closing Comment

by:TuringMachine0101
ID: 31573948
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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