(java) Writing in file little endian

后端 未结 3 622
被撕碎了的回忆
被撕碎了的回忆 2021-02-07 07:06

I\'m trying to write TIFF IFDs, and I\'m looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want):

         


        
相关标签:
3条回答
  • 2021-02-07 07:18

    ByteBuffer is apparently the better choice. You can also write some convenience functions like this,

    public static void writeShortLE(DataOutputStream out, short value) {
      out.writeByte(value & 0xFF);
      out.writeByte((value >> 8) & 0xFF);
    }
    
    public static void writeIntLE(DataOutputStream out, int value) {
      out.writeByte(value & 0xFF);
      out.writeByte((value >> 8) & 0xFF);
      out.writeByte((value >> 16) & 0xFF);
      out.writeByte((value >> 24) & 0xFF);
    }
    
    0 讨论(0)
  • 2021-02-07 07:18

    Check out ByteBuffer, specifically the 'order' method. ByteBuffer is a blessing for those of us who need to interface with anything not Java.

    0 讨论(0)
  • 2021-02-07 07:27

    Maybe you should try something like this:

    ByteBuffer buffer = ByteBuffer.allocate(1000); 
    buffer.order(ByteOrder.LITTLE_ENDIAN);         
    buffer.putChar((char) 12);                     
    buffer.putChar((char) 259);                    
    buffer.putChar((char) 3);                      
    buffer.putInt(1);                              
    buffer.putInt(1);                              
    byte[] bytes = buffer.array();     
    
    0 讨论(0)
提交回复
热议问题