4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

printf の幅指定で "%0*x" を使う

Posted at

C言語の printf や ruby の 文字列%配列 などで、"%0*x" のような書式指定が使える。

"%0*x"の使用例
for( i=3 ; i<8 ; ++i ){
  printf( "%0*x\n", i, 0x123 );
}

を実行すると、

出力結果
123
0123
00123
000123
0000123

となる。
つまり、"%0*x", 5, 0x123 は、"%05x", 0x123 と同じ。

古来より使える機能なので %d や %x などが使える書式指定文字列ならみんな使える。
と思っていたのだが。
java の String.format ではサポートされていない模様。残念。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?