unsigned int into byte[4]

P

palmis

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?
 
G

Gordon Beaton

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 = val & 0xff;
val >>= 8;
}

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

/gordon
 
P

palmis

Dear gordon,
Thanks but long var is segned. I want to use my Unsegned Integer32.
This is my problem.
 
T

Thomas Weidenfeller

palmis said:
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
 
G

Gordon Beaton

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
 
R

Roedy Green

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
 
P

palmis

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top