Home  >  Q&A  >  body text

java - 十六进制转八进制

问题描述
  给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
  输入的第一行为一个正整数n (1<=n<=10)。
  接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出格式
  输出n行,每行为输入对应的八进制正整数。
注意
  输入的十六进制数不会有前导0,比如012A。
  输出的八进制数也不能有前导0。
样例输入
2
39
123ABC
样例输出
71
4435274
提示
  先将十六进制数转换成某进制数,再由某进制数转换成八进制。

黄舟黄舟2717 days ago589

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:19:16

    Simple method: convert hexadecimal to binary and then to octal

    You can convert hexadecimal to binary first
    123ABC -> 0001 0010 0011 1010 1011 1100
    Then look at the binary sequence in groups of three (from the lowest bit)
    0001 0010 0011 1010 1011 1100 -> 000 100 100 011 101 010 111 100
    Then convert the binary sequence of three groups into octal
    000 100 100 011 101 010 111 100 -> 04435274
    Just discard the highest zero

    Binary has a corresponding relationship with hexadecimal and octal

    二进制 十六进制
    0000 0
    0001 1
    0010 2
    0011 3
    0100 4
    0101 5
    0110 6
    0111 7
    1000 8
    1001 9
    1010 A
    1011 B
    1100 C
    1101 D
    1110 E
    1111 F
    二进制 八进制
    000 0
    001 1
    010 2
    011 3
    100 4
    101 5
    110 6
    111 7

    ps I think this level of problems should not appear in this community.

    reply
    0
  • Cancelreply