unsigned int into byte[4]

Discussion in 'Java' started by palmis, Dec 15, 2005.

  1. palmis

    palmis Guest

    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?
     
    palmis, Dec 15, 2005
    #1
    1. Advertising

  2. 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 = 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
     
    Gordon Beaton, Dec 15, 2005
    #2
    1. Advertising

  3. palmis

    palmis Guest

    Dear gordon,
    Thanks but long var is segned. I want to use my Unsegned Integer32.
    This is my problem.
     
    palmis, Dec 15, 2005
    #3
  4. 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/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Dec 15, 2005
    #4
  5. 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
     
    Gordon Beaton, Dec 15, 2005
    #5
  6. palmis

    Roedy Green Guest

    On 15 Dec 2005 08:22:58 -0800, "palmis" <>
    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.
     
    Roedy Green, Dec 16, 2005
    #6
  7. palmis

    palmis Guest

    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?
     
    palmis, Dec 16, 2005
    #7
  8. palmis

    Guest

    , Dec 16, 2005
    #8
  9. palmis

    palmis Guest

    palmis, Dec 16, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    976
    Bob Hairgrove
    Oct 30, 2004
  2. er
    Replies:
    6
    Views:
    500
    Andre Kostur
    Sep 14, 2007
  3. fancyerii
    Replies:
    21
    Views:
    1,550
    Roedy Green
    Nov 5, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    419
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    762
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page