logo

Java 16진수를 10진수로 변환

우리는 변환할 수 있습니다 자바에서 16진수를 10진수로 사용하여 정수.parseInt() 메서드 또는 사용자 정의 논리.

Java 16진수를 10진수로 변환: Integer.parseInt()

Integer.parseInt() 메소드는 주어진 redix를 사용하여 문자열을 int로 변환합니다. 그만큼 서명 ParseInt() 메소드는 다음과 같습니다:

 public static int parseInt(String s,int redix) 

Java에서 16진수를 10진수로 변환하는 간단한 예를 살펴보겠습니다.

 public class HexToDecimalExample1{ public static void main(String args[]){ String hex='a'; int decimal=Integer.parseInt(hex,16); System.out.println(decimal); }} 
지금 테스트해보세요

산출:

 10 

Integer.parseInt() 메소드의 또 다른 예를 살펴보겠습니다.

 public class HexToDecimalExample2{ public static void main(String args[]){ System.out.println(Integer.parseInt('a',16)); System.out.println(Integer.parseInt('f',16)); System.out.println(Integer.parseInt('121',16)); }} 
지금 테스트해보세요

산출:

 10 15 289 

Java 16진수를 10진수로 변환: 사용자 정의 논리

우리는 변환할 수 있습니다 자바에서 16진수를 10진수로 사용자 정의 논리를 사용합니다.

 public class HexToDecimalExample3{ public static int getDecimal(String hex){ String digits = &apos;0123456789ABCDEF&apos;; hex = hex.toUpperCase(); int val = 0; for (int i = 0; i <hex.length(); 121 i++) { char c="hex.charAt(i);" int d="digits.indexOf(c);" val="16*val" + d; } return val; public static void main(string args[]){ system.out.println('decimal of a is: '+getdecimal('a')); f '+getdecimal('f')); '+getdecimal('121')); }} < pre> <span> Test it Now </span> <p>Output:</p> <pre> Decimal of a is: 10 Decimal of f is: 15 Decimal of 121 is: 289 </pre></hex.length();>