Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion of Big Endian to Little Endian Vise versa  RSS feed

 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have data of byte array which in a Big Endian order i want to convert into Little Endian.
Can you anybody help

Thanks
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

Is there any possiblity to convert without using API like ByteBuffer.


 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a byte array just reverse the array:
 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

instead of reversing i shifted the bytes and got the output,

firstByte << 24 ;
secondByte << 16 ;
thirdByte << 8 ;
fourthByte ;

Thanks a lot for response
 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to send a int value to server in little endian so it should be converted to uint32. I have this method to convert from big endian to little endian. Can I run an endian converter on a byte array? Please let me know.

public static byte[] convertBigEndianToLittleEndianByteArray(int i)
{
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.LITTLE_ENDIAN).putInt(i);
byte [] b = buffer.array();
System.out.print("Little endian buffer of " + i + " = ");
for(int x=0; x<b.length; x++) System.out.print(b[x] + " ");
System.out.println();
return b;
}

Thanks,
 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please help me on this question? I need to get this done.

Thanks,
 
Stephan van Hulst
Saloon Keeper
Posts: 6976
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look here:
http://www.coderanch.com/t/522479/java/java/Write-Binary-File-Java-read#2366837
 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!