<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 程序:十六進制到十進制的轉換 > 原文: [https://beginnersbook.com/2019/04/java-hexadecimal-to-decimal-conversion/](https://beginnersbook.com/2019/04/java-hexadecimal-to-decimal-conversion/) 在本指南中,我們將在示例的幫助下,學習如何**將十六進制轉換為十進制**數字。 ## 十六進制到十進制轉換示例 我們可以簡單地使用`Integer.parseInt()`方法并將基數傳遞為 16 以將給定的十六進制數轉換為等效的十進制數。 這里我們給出了一個十六進制數`hexnum`,我們通過使用`Integer.parseInt()`方法將其轉換為十進制數,并將基數傳遞為 16。 ```java public class JavaExample{ public static void main(String args[]){ //given hexadecimal number String hexnum = "6F"; //converting hex to decimal by passing base 16 int num = Integer.parseInt(hexnum,16); System.out.println("Decimal equivalent of given hex number: "+num); } } ``` **輸出:** ![Java hexadecimal to decimal example](https://img.kancloud.cn/90/80/908069829b8f2cc5564b6cf634c405c5_1024x513.jpg) ## 基于用戶輸入的十六進制到十進制轉換 在上面的例子中,我們給出了一個數字。如果我們想要我們可以從用戶那里獲得輸入,然后我們可以使用我們上面使用的相同邏輯將輸入的十六進制數轉換為十進制數。 ```java import java.util.Scanner; public class JavaExample{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); System.out.print("Enter any hexadecimal number: "); String hexnum = scanner.nextLine(); scanner.close(); //converting hex to decimal by passing base 16 int num = Integer.parseInt(hexnum,16); System.out.println("Decimal equivalent of given hex number: "+num); } } ``` **輸出:** ![Java hexadecimal to decimal example user input](https://img.kancloud.cn/20/7b/207b6cd6cc8ada097f06bd602f171646_1024x632.jpg) ## 使用用戶定義的方法的十六進制到十進制轉換 這里我們沒有使用任何預定義的方法進行轉換,我們正在編寫自己的邏輯來將給定的十六進制數轉換為十進制數。我們在用戶定義的方法`hexToDecimal()`中編寫了轉換邏輯。此示例還使用[`String`類](https://beginnersbook.com/2013/12/java-strings/)的[`charAt()`](https://beginnersbook.com/2013/12/java-string-charat-method-example/)和[`indexOf()`](https://beginnersbook.com/2013/12/java-string-indexof-method-example/)方法。 ```java public class JavaExample{ public static int hexToDecimal(String hexnum){ String hstring = "0123456789ABCDEF"; hexnum = hexnum.toUpperCase(); int num = 0; for (int i = 0; i < hexnum.length(); i++) { char ch = hexnum.charAt(i); int n = hstring.indexOf(ch); num = 16*num + n; } return num; } public static void main(String args[]){ System.out.println("Decimal equivalent of 7A is: "+hexToDecimal("7A")); } } ``` **輸出:** ![Java hexadecimal to decimal using custom logic](https://img.kancloud.cn/f0/a2/f0a2afe0a4e358413b8e51cfc5632b2b_1024x667.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看