aspose file tools*
The moose likes Beginning Java and the fly likes Please explain Integer.valueOf( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain Integer.valueOf("100",2) how it works??" Watch "Please explain Integer.valueOf("100",2) how it works??" New topic
Author

Please explain Integer.valueOf("100",2) how it works??

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Hi Guys!
I have a silly doubt about Integer class static method valueOf(String s,int radix) ,let me show you the sample code ...


here i'm not getting how the valueOf() is working and how the output is producing, i have searched about my this problem but i did't get right answer whatever want to know ,so please Guys help me to clear my doubt ,i would be very thankful for your reply.
thanks in advance..

regards:
Vivek
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 241
    
    1

The first parameter is a String that is formatted like an int, and the second is the base system being used to produce the value of the String. In the case of "100" and 2, you're saying that you want to get the value of 100 in binary. If you know anything about base systems, it's simple math: (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 4 + 0 + 0 = 4.

Let's do 101 in the base 4 system: (1 * 4^2) + (0 * 4^1) + (1 * 4^0) = 16 + 0 + 1 = 17.

Make sense?
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Joel Christophel wrote:The first parameter is a String that is formatted like an int, and the second is the base system being used to produce the value of the String. In the case of "100" and 4, you're saying that you want to get the value of 100 in binary. If you know anything about base systems, it's simple math: (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 4 + 0 + 0 = 4.

Let's do 101 in the base 4 system: (1 * 4^2) + (0 * 4^1) + (1 * 4^0) = 16 + 0 + 1 = 17.

Make sense?




Thank you very much Joel, you explained well ,it's much enough to understand the logic behind this, thanks for giving your valuable time to explain this thread.

-vivek
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Did you look in the API documentation? This link and the links in it, explains your question.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

Joel Christophel wrote: In the case of "100" and 4, you're saying that you want to get the value of 100 in binary.

Don't you mean the case of "100" and 2, you want it in binary?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 241
    
    1

fred rosenberger wrote:Don't you mean the case of "100" and 2, you want it in binary?


Good catch, sir.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain Integer.valueOf("100",2) how it works??