Solved

What is this formatting? System.out.format...

Posted on 2011-09-07
10
335 Views
Last Modified: 2012-05-12
System.out.format(" %1$02x", ..... );

Open in new window


Can anyone explain what is this formatting?
0
Comment
Question by:humansg
  • 8
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36500586
It is probably System.out.printlf formatting the same a in C languhge
look ath these examples:
http://www.java2s.com/Code/JavaAPI/java.lang/System.out.printf.htm
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500589
this is format struing syntaz
the same for System.out.printf or Systemm.out.format

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36500597

Argument Index

especially look at this in your case
The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

Another way to reference arguments by position is to use the '<' ('\u003c') flag, which causes the argument for the previous format specifier to be re-used. For example, the following two statements would produce identical strings:

       Calendar c = ...;
       String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);

       String s2 = String.format("Duke's Birthday: %1$tm %<$te,%<$tY", c);

So it means that the first varuiable should be printed as heaxadecimal - two charctes - that;'sa my understanding:
System.out.format(" %1$02x", ..... );


     
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500625
So you see if I print 16 it will print 10 (in hexadecimal - two positoions, as exopected)

          int mynum = 16;
System.out.format(" %1$02x",mynum );

Open in new window


Output:
 10

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500628


And this is how it prints number 11 in hexadecimal - occupying two positions
          int mynum = 11;
System.out.format(" %1$02x",mynum );

Open in new window


 0b

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:humansg
ID: 36500748
Hi for_yan,

this is the formatting syntax.
%[argument_index$][flags][width]conversion

"%1$02x"
[argument_index$] is 1$    (which is the first argument <- understood)
[flags] is "0"                        (what is this flag about?)
[width] is "2"                       (2 character length?)
conversion is "x"                 (which is to hexadecimal base 16 <- understood)

The description used for flag is "The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion."
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500758
Yes, 2 is charcter length for sure, I think zero is not a flag, but just part of the width 02 -  ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500761


Flag  General  Character  Integral  Floating Point  Date/Time  Description  
'-'  y  y  y  y  y  The result will be left-justified.  
'#'  y1  -  y3  y  -  The result should use a conversion-dependent alternate form  
'+'  -  -  y4  y  -  The result will always include a sign  
'  '  -  -  y4  y  -  The result will include a leading space for positive values  
'0'  -  -  y  y  -  The result will be zero-padded  
','  -  -  y2  y5  -  The result will include locale-specific grouping separators  
'('  -  -  y4  y5  -  The result will enclose negative numbers in parentheses  

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36500765
No, actually in this case flag is zero - result will be zero-padded - indeed "0b"
0
 

Author Closing Comment

by:humansg
ID: 36500769
Yeah, I got that too!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now