programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# long to Integer conversion

Nicky narayan
Greenhorn
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
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?

Nicky narayan
Greenhorn
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
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?

Nicky narayan
Greenhorn
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
Posts: 23295
125
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

W. Joe Smith
Ranch Hand
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
Sheriff
Posts: 16060
88
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.

Campbell Ritchie
Marshal
Posts: 56536
172
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
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.

Rob Spoor
Sheriff
Posts: 21135
87
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.

Mario Busch
Greenhorn
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
Marshal
Posts: 56536
172
Mario Busch: welcome to the Ranch

Campbell Ritchie
Marshal
Posts: 56536
172
You can probably do the fragmenting of a long into two ints with the >> operator and 0x20 (= 32) and two casts.