This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes how 0x7f =127 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how 0x7f =127" Watch "how 0x7f =127" New topic
Author

how 0x7f =127

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

I am not able to understand conversion for 0x7f to 127. I do understand hexadecimal system.. but for this conversion I am confused please help


No Kaustubh No Fun, Know Kaustubh Know Fun..
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
7*16 + 15 = 127
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

ohk but 16 represent which char? and there's no addition sign between them... what is the logic behind it?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
16 is the base of the hexadecimal system.

Think of how you would convert 11011 from binary to decimal.

1*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0 = 16 + 8 + 0 + 2 + 1 = 27
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

See: Hexadecimal


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Kaustubh G Sharma wrote:ohk but 16 represent which char? and there's no addition sign between them... what is the logic behind it?


How does 23 (in decimal) = 23? Because it is (2 * 10) + 3.

The first digit is 2, which is in the 10s position. So to get the value of the first digit you do (2 * 10). The second digit is in the ones position, and has a value of 3. To get the total value of the number you sum the values of the digits, so 23 == (2 * 10) + 3.

Since you understand Hexadecimal you should be able to apply the same conversion to 0x7f.

Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Kaustubh G Sharma wrote:I am not able to understand conversion for 0x7f to 127. I do understand hexadecimal system.. but for this conversion I am confused please help

I fear that several people (including me) are having a problem understanding your problem. If you "do understand hexadecimal system", then surely you understand that '7f' can't be anything but 127 decimal?

Or is it the '0x' bit that is bothering you? That is simply a prefix that tells the Java compiler that the characters that follow are hexadecimal.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Winston Gutkowski wrote:
Kaustubh G Sharma wrote:I am not able to understand conversion for 0x7f to 127. I do understand hexadecimal system.. but for this conversion I am confused please help

I fear that several people (including me) are having a problem understanding your problem. If you "do understand hexadecimal system", then surely you understand that '7f' can't be anything but 127 decimal?

Or is it the '0x' bit that is bothering you? That is simply a prefix that tells the Java compiler that the characters that follow are hexadecimal.

Winston


Yes Winston I was confused because of this 0x thing.. Now my doubt is clear.. Thanks to all of you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how 0x7f =127
 
Similar Threads
Problem converting C/C++ unsigned char to JAVA
convert character to integer
Hexa / Octa Conversions.
Regading Byte.MAX_VALUE
More Silly Hex to Byte to String and Back Conversions