This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Converting Interger to short Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting Interger to short" Watch "Converting Interger to short" New topic
Author

Converting Interger to short

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Hi all,
I am porting my code base from c++ to JAVA, I want to convert an integer to short, to insert it into byte array.
e.g.




can you please help me with this..??


Sage of The Monstrous Toad of Mount Myoboku
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Can you show how you were doing it in C++, I mean how you were dividing the unsigend short into to bytes.


Piyush
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Following code demonstrate c++ code
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
Hello. I think this will solve your problem:



Warning: this code will not account for signed values. To account for them use 0xffff0000 in the high assignment.
Obviously you can assign directly to the buffer, but i think by presenting the code like this everyone will understand a bit better what is going on.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Well, that ain't going to work at all well in Java™ because you can't write (char*) and you can't write &v.

Write down the representation of a Java™ int on paper with a pencil (as Fred always says), and get a large eraser in case anything goes wrong.
Similarly write down a byte, underneath the int.
Now you see, you have one number with 32 bits and another with 8 bits.
Now can you work out how to get the right-most 8 bits (bits 0-7 inclusive) out of the 32 bit number? An experienced chap would use the bitwise operators, but the arithmetic operators will do it too.
Now you have an int representing the right-most 8 bits of your original int. There remain two things to do.
  • 1: Cast the resultant int to a byte.
  • 2: Repeat with the remainder of the int until you have all four bytes out of it. You will probably need to move all the bits 8 places to the right somewhere in this operation.


  • Remember that an int might be signed or unsigned in C/C++ and it might have 16 bits or 32, but in Java™ it is always signed (two's complement) and 32 bits. If you know you only need 16 bits, then you will only need two bytes.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37926
        
      22
    Why on earth are you using decimal arithmetic, Daniel Marti? You should write those numbers in hexadecimal, which is much less error-prone.
    Daniel Marti
    Ranch Hand

    Joined: Jun 08, 2011
    Posts: 37
    Campbell Ritchie wrote:
    Now you see, you have one number with 32 bits and another with 8 bits.


    I believe a short is a 16 bit number representation right? Or am i missing something?

    Campbell Ritchie wrote:Why on earth are you using decimal arithmetic, Daniel Marti? You should write those numbers in hexadecimal, which is much less error-prone.

    True, but if any reader wants to understand what is going on on that piece of code, if they try to check the size of int in java they will get a decimal value, not hexadecimal.
    And i kind of understand decimal/octal better than hexadecimal ( i know i know, shame on me).
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18507
        
      40


    The java core library also has the java.nio.ByteBuffer class -- which supports getting data in and out of byte arrays.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37926
        
      22
    Daniel Marti wrote: . . . I believe a short is a 16 bit number representation right? Or am i missing something?
    Yes, a short is 16 bits or two bytes or four nybbles.
    The code shown is ambiguous, however; it says "int" mostly. But it doesn't matter; you can use the same technique for longs shorts and ints, just using different size arrays.

    I did say "If you know you only need 16 bits, then you will only need two bytes," thinking that would cover a short.
    . . . i kind of understand decimal/octal better than hexadecimal ( i know i know, shame on me).


    You only need one hex number, 0xff, and you can even substitute Byte.MIN_VALUE for that.There are also methods in java.lang.Integer which allow bit-twiddling, eg rotateXXX, which might be useful here.
     
    wood burning stoves
     
    subject: Converting Interger to short
     
    Similar Threads
    16 bit char ???
    Convert small C routine to Java
    CRC16 with Polynominal : 0x8408, and Preset CRC : 0x6363
    Java endian and bit order and signed and unsigned and python and more confusion than I can deal with
    Convert hex to decimal value