How to Print Hexadecimal Numbers in PHP or Java

时光总嘲笑我的痴心妄想 提交于 2019-11-30 12:34:40

You need to print the numbers 1 to 30 in hexadecimal notation. Try this method for each line:

dechex ( int $number )

For Java:

System.out.println(Integer.toHexString(number));

or

System.out.println(String.format("%x", number));

The latter has more options for formatting the hex string.

objects
    for ( int i=1 ; i <= x; i++ ) {
        System.out.printf("%02x\n", i);
    }

This will print hexadecimal 01-24 (with 0 padding in front of numbers less than 10)

for ($i = 1; $i <= 36; $i++) {
    printf("%02x\n", $i);
}
<?php
function blah($n) {
  for($i=1;$i<=$n;$i++) {
    printf("%02x\n", $i);
  }
}

blah(36);
?>

01
02
03
04
05
06
07
08
09
0a
0b
0c
0d
0e
0f
10
11
12
13
14
15
16
17
18
19
1a
1b
1c
1d
1e
1f
20
21
22
23
24
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!