The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
June 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

nothl and htonl

Posted by Sugnesh Patel on August 26, 2001 at 7:31 AM

There is no such function in java and if you try to read first into byte array and then try to interpret as int like following code

long lntohl = 0;

lntohl = byteA[0] << 24;
lntohl += byteA[0] << 16;
lntohl += byteA[0] << 8;
lntohl += byteA[0]24;

if you do this and any of the above byte has value 0XF*, then it is going to crap out.

use this code if you want four byte into byte array as well as in long

first mark position of buffer at 0 byte, then read four byte into array. after this call fun reser to set buffer pointer at 0 byte and use following code to get long value.

long lValue = reply.read() << 24;

lValue += reply.read() << 16;

lValue += reply.read() << 8;

lValue += reply.read() ;

This will give you exact answer.

But I do not like Java, because it is UGLY programming language, you can not do whatever you want unlike C++.


Regards

Sugnesh Patel




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us