Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting Interger to short

 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..??
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show how you were doing it in C++, I mean how you were dividing the unsigend short into to bytes.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code demonstrate c++ code
 
Daniel Marti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Pie
    Posts: 21000
    76
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

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

    Henry
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic