aspose file tools*
The moose likes Beginning Java and the fly likes long to Integer conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "long to Integer conversion" Watch "long to Integer conversion" New topic
Author

long to Integer conversion

Nicky narayan
Greenhorn

Joined: Jul 01, 2009
Posts: 27
Hi,

I am trying to convert an long value to Intger.
Like:
long =1282900123450l;
System.out.println("x value is -->"+x);
Integer y=(int)x;
System.out.println("y value is -->"+y);

The result is :
x value is -->1282900123450
y value is -->-1295098054


can anyone help me on this how to convert long to Integer value without getting -ve value.It is very urgent to me.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
In your above code you are getting different answers because your long is too big to fit in an int, so when you cast your long to an int it is chopping off bits to make it fit. May I ask why you have to convert a long to an Integer?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Nicky narayan
Greenhorn

Joined: Jul 01, 2009
Posts: 27

I am getting Date variable as a long value
After that i have to convert it to Integer for my requirement.

Please suggest me how to convert it.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Nicky narayan wrote:
I am getting Date variable as a long value
After that i have to convert it to Integer for my requirement.

Please suggest me how to convert it.


If the value is bigger than an int range, what is the purpose of using int/Integer there?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Nicky narayan
Greenhorn

Joined: Jul 01, 2009
Posts: 27
i have to convert it like that only

is there any chance to avoid -ve value while converting long to int
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Nicky narayan wrote:i have to convert it like that only

is there any chance to avoid -ve value while converting long to int



Think about what you are asking. If I asked you to convert the number 42 to a single digit number, can you do it? And if you can't, how do you expect the computer to do it?

In this case, you are asking to convert a big number to a type that can't hold that number, how do you expect the computer to do it?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
From my understanding, if your long is small enough to fit into an int I THINK (emphasis on that, because I'm not sure) that you can cast it to an int and it might retain the same value. I'm not sure if it drops the bits from the left or right (I believe it is left). However, if you have a value too big for an int and cast it to an int, I'm pretty sure there is no way to keep the value. At that point, you are asking an int to hold something too big for it, which isn't possible.

Edit: I still recommend against the cast. It's the same as trying to put all the water currently in a 10 gallon bucket into a 5 gallon bucket. Sure the 10 gallon may only be holding 4 gallons, but there is no way to be sure it isn't holding over 5 gallons.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

A long is 64 bits. An int is 32 bits. Not all values that fit in a long will fit in an int, because the int is smaller. If the number is too large to fit in an int or Integer, there is no way to make it fit.

Dates are often stores as 64-bit numbers. You can't fit those in an int.

So, there is no solution to your problem. You need to talk to whoever gave you the requirements and explain that it's not possible.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Nicky narayan wrote: . . . It is very urgent to me.
We don't use the word "urgent" on this website.

Also, to be pedantic, you should distinguish int from Integer and from integer. They all mean different things to Java programmers.
Daniel Gen Li
Ranch Hand

Joined: Jan 02, 2010
Posts: 32
lol. it's true that I don't see why you'd desperately want to convert the long into an int when it's going to be truncated. but for future reference. If you want to see if a value in a long will fit into an int, you can check by conparing your long to Integer.MAX_VALUE or Integer.MIN_VALUE.


Li
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

W. Joe Smith wrote:From my understanding, if your long is small enough to fit into an int I THINK (emphasis on that, because I'm not sure) that you can cast it to an int and it might retain the same value. I'm not sure if it drops the bits from the left or right (I believe it is left). However, if you have a value too big for an int and cast it to an int, I'm pretty sure there is no way to keep the value. At that point, you are asking an int to hold something too big for it, which isn't possible.

You're completely right on all accounts. Truncating is always done from the left.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mario Busch
Greenhorn

Joined: Sep 02, 2010
Posts: 1
if its - however - needed to get it into a int, fragment the long-parts of the date you need for your int (depending on your needs which we dont know) and put only those parts into the int you really need. maybe you only need ddmmyyyy f.e.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Mario Busch: welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can probably do the fragmenting of a long into two ints with the >> operator and 0x20 (= 32) and two casts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: long to Integer conversion