Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > unsigned int into byte[4]

Reply
Thread Tools

unsigned int into byte[4]

 
 
palmis
Guest
Posts: n/a
 
      12-15-2005
Hi, I have a problem.
I want to put my unsigned int into a byte[4].
this is my class:


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class UnsignedInt32 extends UnsignedInt implements Serializable
{

final static long serialVersionUID = 200;

/**
* the maximum value this long can have
*/
public final static long MAX_VALUE = 0xffffffffL;

/**
* the minimum value this long can have
*/
public final static long MIN_VALUE = 0;

/**
* Constructor creates an unsigned 32-bit integer object for
* the specified long value. Only the bottom 32 bits are
* considered.
*
* @param value the long to be represented as an unsigned 32-bit
integer
* object
*/
public UnsignedInt32(long value) {
if ((value < MIN_VALUE) || (value > MAX_VALUE)) {
throw new NumberFormatException();
}
this.value = new Long(value);
}

/**
* Constructor creates an unsigned 32-bit integer object for the
specified
* string. Only the bottom 32 bits are considered.
*
* @param value the string to be represented as an unsigned 32-bit
integer
* @throws NumberFormatException if the number is out of range
*/
public UnsignedInt32(String value) throws NumberFormatException {
Long temp = new Long(value);
long longValue = temp.longValue();
if ((longValue < MIN_VALUE) || (longValue > MAX_VALUE)) {
throw new NumberFormatException();
}
this.value = temp;
}

}



I have found this method:

/**
* Uses an output stream to convert an int to four bytes (INT32).
*/
public static byte[] intToFourBytes(int i) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(i);
baos.close();
dos.close();
byte[] retArray = baos.toByteArray();
return(retArray);
}

but it uses int with sign in java. How can I do my method that uses
UnsignedInt32?

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      12-15-2005
On 15 Dec 2005 07:15:45 -0800, palmis wrote:
> I want to put my unsigned int into a byte[4].


First, decide in what order the 4 bytes should end up in the array
(that really depends on what you need to do with it later).

Then realize that you can extract any single byte from the long by
first right-shifting it 0, 8, 16 or 24 bits, then masking the
resulting value with 0xff to isolate the byte.

Here's one way (this puts the bytes in big endian order):

long val = someValue;
byte[] bytes = new byte[4];

for (i=3; i>=0; i++) {
bytes[i] = val & 0xff;
val >>= 8;
}

To get little endian instead, iterate from 0-3.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
palmis
Guest
Posts: n/a
 
      12-15-2005
Dear gordon,
Thanks but long var is segned. I want to use my Unsegned Integer32.
This is my problem.

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      12-15-2005
palmis wrote:
> public class UnsignedInt32


This looks as if you are trying to fight Java's type system. No, wait,
we just recently went through the signed/unsigned issue with you.
Apparently, you chose to ignore the advice you got. There is really not
much need in Java to build an own unsigned type. Particular, because
your implementation wastes a lot of space in relation to a simple 32 bit
int (an object using another object (a Long) to finally wrap a 64 bit
long of which actually just 32 bits are used).


> DataOutputStream dos = new DataOutputStream(baos);


DataOutputStream.writeInt() doesn't care about the sign of an int. It
just writes out 32 bits. Just use it as-is to write "unsigned" ints. It
has been explained to you why you can do this the last time.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      12-15-2005
On 15 Dec 2005 08:22:58 -0800, palmis wrote:
> Thanks but long var is segned. I want to use my Unsegned Integer32.
> This is my problem.


In the code you posted, the UI32 apparently uses a Long to hold the
actual value. So write a method that gets the long value from the
Long, then uses the technique I showed you.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-16-2005
On 15 Dec 2005 08:22:58 -0800, "palmis" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Thanks but long var is segned. I want to use my Unsegned Integer32.
>This is my problem.




You can store a signed or unsigned number in byte, int or long. If you
chop the extended bits on unsigned byte load ( & 0xff ), and confine
yourself to operators + - >>> << & | ~ you can pretty much treat them
as if they were unsigned. It is only when you compare, multiply or
divide with them does it matter if the value is signed or unsigned.

see http://mindprod.com/jgloss/unsigned.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
palmis
Guest
Posts: n/a
 
      12-16-2005
Dear Thomas,
I have intentional to create one characteristic
UnsignedInteger32 class because this is of the programmings object
oriented characteristic. Not to think that I do not have intentional to
use your
council. The problem is that I have the confused ideas much in merit.
There is who speaks me about complement to two, who of masks. Then on
Internet I have found the source of this class and have thought to
have resolved my problems, but is not thus.
You could tell me which would be the better solution?

 
Reply With Quote
 
ricky.clarkson@gmail.com
Guest
Posts: n/a
 
      12-16-2005
Palmis,

This is not a criticism. It seems like you are using a translating
tool that translates to Shakespearian English. If you are, I want the
name, I love it.

You should read this forum post, which I found trivially using google:

http://forum.java.sun.com/thread.jsp...sageID=1209403

Cheers.

 
Reply With Quote
 
palmis
Guest
Posts: n/a
 
      12-16-2005
translating site:
http://freetranslation.paralink.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
int*unsigned int = unsigned? ciccio C++ 2 06-04-2010 01:54 PM
How to combine 2 unsigned short into a unsigned int? fancyerii Java 21 11-05-2007 09:33 PM
is vec.reserve(unsigned int) better than vec(unsigned int)? er C++ 6 09-14-2007 06:20 AM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM



Advertisments