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