• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number formatting  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Java so probably this is an easy question for some of you out there. I am currently reading HeadFirstJava, and there is a thing I do not unders about number formatting.

As can be seen in the attached document, the writer explains about %d and %f, which I understand. However, I do not understand how he gets 2a from format("%x", 42);. Same with * from format("%c", 42).

1) What happens when putting "%x" and "%c" into the format?
2) Why does it give these outputs?
3) Why do the type have to be a byte, short, int, long or BigInterger (what that might be)?

Thank you guys in advance.

Bart
Page.PNG
[Thumbnail for Page.PNG]
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:I do not understand how he gets 2a from format("%x", 42)

42 in Hexadecimal format is 2a (2 = 2*16; a=10*1 where 10 is represented by a in Hex)
Bart Boersma wrote:Same with * from format("%c", 42)

* in char format is represented by 42, alternatively 42 is represented by *. For info A-Z in char is 65-91. This can be handy to know when wanting to use a letter as method to derive an array index.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) What happens when putting "%x" and "%c" into the format?
> The %x is format specifier to print the hexadecimal value for the corresponding value. Try experiment it yourself. Use a online hex to decimal converter try 2a as your hexadecimal which would get printed as 42.
http://www.binaryhexconverter.com/hex-to-decimal-converter

With the %c format specifier, to convert decimal to ascii equivalent character. Same use online ascii table
http://www.asciitable.com/
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Both of you thanks for the input. This leaves me with just one question that Paul tried to explain already:

1) What is the use of converting this into hexadecimal numbers of ascii equivalent characters?

Cheers
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:1) What is the use of converting this into hexadecimal numbers of ascii equivalent characters?
HeadJava will just be using this as an example of what you can do. When and where you would need to do it is up to whatever applications you work with i.e. whether there will be a need to hold numbers in Hex format.
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I will be putting this thread to resolved.

Cheers
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:Hey guys,

Both of you thanks for the input. This leaves me with just one question that Paul tried to explain already:

1) What is the use of converting this into hexadecimal numbers of ascii equivalent characters?

Cheers

As I recall, and this was ~35 years ago, hex was used quite a bit in assembly language.  I found this tidbit over on Stack Overflow
But because representing a binary number to humans as a string of 0 and 1 makes them difficult to read, hex is used as a shortcut: groups of 4 bits (those 1 and 0 informations) are represented as one hex digit. This conversion from binary to hex, and vice versa, is very simple. Converting binary to decimal, or decimal to binary, involved more actions.

Therefore, the hex system isn't used by computers, but by humans, for terser representation of binary numbers, but easier convertibility than from/to decimal number system.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!