Results 1 to 7 of 7
  1. #1
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default convert unsigned integer to signed integer in java?

    is there a way to convert unsigned integer to signed integer in java?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    is there a way to convert unsigned integer to signed integer in java?
    Java doesn't have unsigned integers; the only unsigned quantity is the char type. What do you need it for?

    kind regards,

    Jos

  3. #3
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    i wish to do the following.
    i managed to do some computation to get to the following hexi
    0x8765 ==> 1000 0111 0110 0101

    but i wish to convert it to
    0xFFFF8765 ==> 1111 1111 1111 1111 1000 0111 0110 0101


    but if it is
    0x4321 ==> 0100 0011 0010 0001

    i wish to produce the same
    0x4321 ==> 0000 0000 0000 0000 0100 0011 0010 0001

    How can i do it without much steps...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    i wish to do the following.
    i managed to do some computation to get to the following hexi
    0x8765 ==> 1000 0111 0110 0101

    but i wish to convert it to
    0xFFFF8765 ==> 1111 1111 1111 1111 1000 0111 0110 0101


    but if it is
    0x4321 ==> 0100 0011 0010 0001

    i wish to produce the same
    0x4321 ==> 0000 0000 0000 0000 0100 0011 0010 0001

    How can i do it without much steps...
    You can use shorts for that purpose; shorts are also signed and sign extension (that is what you want) is built in. Parse your shorts as if they were ints and cast them to a short afterwards.

    kind regards,

    Jos

  5. #5
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    thanks. it works
    short is 16 bits?
    can u guide me on how by converting to short will make this work?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    thanks. it works
    short is 16 bits?
    can u guide me on how by converting to short will make this work?
    Yep, a short has 16 bits. Converting it to an int uses sign extension when applicable; have a look:

    Java Code:
    int source= 0x4123;
    short ssource= (short)source;
    int target= ssource; // == 0x00004123
    
    int source= 0x8123;
    short ssource= (short)source;
    int target= ssource; // == 0xffff8123
    kind regards,

    Jos

  7. #7
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    it is surprisingly that short will pick up the 16th binary and convert the front bits according to whether it is 1 or 0...

    Does this apply to all other datatype?

    And what if it's vice versa, when it's 16bit changing to 32?
    it will also see the 16th bit and change accordingly?

Similar Threads

  1. How to convert Integer[] to int[]
    By Nithya in forum New To Java
    Replies: 26
    Last Post: 02-11-2010, 06:41 PM
  2. how to convert a string to an integer
    By LAW in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:29 AM
  3. Replies: 10
    Last Post: 05-06-2009, 10:30 PM
  4. Replies: 0
    Last Post: 04-06-2008, 08:40 PM
  5. Help with Integer in java
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-14-2007, 06:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •