Decimal to Hexadecimal Converter in Java

后端 未结 13 2400
有刺的猬
有刺的猬 2020-12-03 13:57

I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a he

13条回答
  •  无人及你
    2020-12-03 14:40

    Another possible solution:

    public String DecToHex(int dec){
      char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                  'A', 'B', 'C', 'D', 'E', 'F'};
      String hex = "";
      while (dec != 0) {
          int rem = dec % 16;
          hex = hexDigits[rem] + hex;
          dec = dec / 16;
      }
      return hex;
    }
    

提交回复
热议问题