|
-
July 5th, 2010, 06:58 AM
#1
Convert int of unknown number of bits to 32 bits int
Hi all,
need some help, trying to convert a integer of unknown number of bits to a 32 bit.
For example: 2196550012
Can you some how drop the least significant bits after the 32nd bit?
So you get a new integer with only 32 bits, this I assume will get me a new value for the integer, but I hope it will be approximately the same as before.
Can't find any threads on on this subject, you are welcome to direct me towards one =)
Thank you!
-
July 5th, 2010, 07:12 AM
#2
Re: Convert int of unknown number of bits to 32 bits int
Do you need to covert a String to integer?
By the way "2196550012" can be represented with log2(2196550012) = 31.03.... => 32 bits.
You have 2 options:
1) Use library routines. You probably need Integer.parseInteger(String, int).
2) Write conversion code yourself (This is particularly true if this is a learning exercise and you are just learning to program. Tutors usually ask to do things this way.):
say I have a number: "1234" it is: 4 * 10^0 + 3 * 10^1 + 2 * 10^2 + 1 * 10^3. ^ stands for power.
So write a loop from 0 and up and sum products of decimal digits and powers of 10. (If the number is decimal.)
Last edited by andrey_zh; July 5th, 2010 at 07:20 AM.
-
July 5th, 2010, 07:22 AM
#3
Re: Convert int of unknown number of bits to 32 bits int
Nope it is not a string!
Is looking at different Ip lookup algoritms in a project, and for testing these I shall use real data from existing routing tables, and the number given as example is a destination address converted to an integer see ex. http://www.aboutmyip.com/AboutMyXApp...30.236.177.124
And have been provided with a file with already converted addresses, so would like to convert these numbers to 32 bits int.
-
July 5th, 2010, 07:25 AM
#4
Re: Convert int of unknown number of bits to 32 bits int
[QUOTE=andrey_zh; By the way "2196550012" can be represented with log2(2196550012) = 31.03.... => 32 bits.
[/QUOTE]
Okey, but why does the Eclipse complain about that it out of range? Because it only use up to 2^31-1 bits? What I have read Java does not support unsgined!
-
July 5th, 2010, 07:31 AM
#5
Re: Convert int of unknown number of bits to 32 bits int
2196550012_10 = 82ECB17C_16
82_16 = 130
EC_16 = 236
B1_16 = 177
7C_16 = 124
Seems you have to use masks if you need to split an integer to four octets.
a.b.c.d
Code:
a = (ip >> 24) & 0xFF;
b = (ip >> 16) & 0xFF;
c = (ip >> 8) & 0xFF;
d = (ip ) & 0xFF;
And yes some IP-s will be stored as negative integers.
Tip: read as long, then convert to int:
Code:
long a = 2196550012L;
System.out.println( (int)a );
Gives the result: -2098417284_10 = 82ECB17C_16.
Last edited by andrey_zh; July 5th, 2010 at 07:41 AM.
-
July 5th, 2010, 07:46 AM
#6
Re: Convert int of unknown number of bits to 32 bits int
thank you, will try that =) at once.
-
July 5th, 2010, 07:51 AM
#7
Re: Convert int of unknown number of bits to 32 bits int
Coz 2196550012 is out of range of int type! do like that (int)(2196550012L). If you have it in a file either read as long or as a String, then convert.
-2098417284_10 = 82ECB17C_16.
This is true for 32 bit representation, not mathematically of course.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|