How to Print Hexadecimal Numbers in PHP or Java

后端 未结 5 1161
青春惊慌失措
青春惊慌失措 2021-01-02 02:27

I need to print some data (a little bit strange formatted). I was writing it in PHP with if ($num%10==9) but it was impossible for me to get correct output.

So take

相关标签:
5条回答
  • 2021-01-02 03:07
        for ( int i=1 ; i <= x; i++ ) {
            System.out.printf("%02x\n", i);
        }
    
    0 讨论(0)
  • 2021-01-02 03:08

    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.

    0 讨论(0)
  • 2021-01-02 03:08
    <?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
    
    0 讨论(0)
  • 2021-01-02 03:13

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

    dechex ( int $number )
    
    0 讨论(0)
  • 2021-01-02 03:26

    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);
    }
    
    0 讨论(0)
提交回复
热议问题