给定一个十进制数作为输入,我们需要编写一个程序,将给定的十进制数转换为等效的十六进制数。i、 e将基值为10的数字转换为基值16。
十六进制数使用16个值来表示一个数字。来自 0-9用数字0-9表示,10-15用A-F中的字符表示。
例如:
Input : 116Output: 74Input : 10Output: AInput : 33Output: 21
算法 :
- 将数字除以16后的余数存储在临时变量temp中。如果temp小于10,则在字符数组中插入(48+temp);如果temp大于或等于10,则在字符数组中插入(55+temp)。
- 现在把这个数字除以16
- 重复上述两个步骤,直到数字不等于0。
- 现在按相反顺序打印阵列。
实例
如果给定的十进制数是2545。
第一步 :当2545除以16等于1时,计算余数。因此,temp=1。因为温度低于10。所以,arr[0]=48+1=49=’1’。 第二步 :2545除以16。新的数字是2545/16=159。 第三步 :当159除以16等于15时,计算余数。因此,温度=15。因为温度大于10。所以,arr[1]=55+15=70=’F’。 第四步 :159除以16。新的数字是159/16=9。 第五步 :当9除以16等于9时,计算余数。因此,温度=9。因为温度低于10。所以,arr[2]=48+9=57=’9’。 第六步 :9除以16。新的数字是9/16=0。 第七步 :因为数字变为=0。停止重复步骤,按相反顺序打印阵列。因此,等效的十六进制数是9F1。
下图显示了将十进制数2545转换为等效十六进制数的示例。
下面是上述想法的实现。
C++
// C++ program to convert a decimal // number to hexadecimal number #include <iostream> using namespace std; // function to convert decimal to hexadecimal void decToHexa( int n) { // char array to store hexadecimal number char hexaDeciNum[100]; // counter for hexadecimal number array int i = 0; while (n != 0) { // temporary variable to store remainder int temp = 0; // storing remainder in temp variable. temp = n % 16; // check if temp < 10 if (temp < 10) { hexaDeciNum[i] = temp + 48; i++; } else { hexaDeciNum[i] = temp + 55; i++; } n = n / 16; } // printing hexadecimal number array in reverse order for ( int j = i - 1; j >= 0; j--) cout << hexaDeciNum[j]; } // Driver program to test above function int main() { int n = 2545; decToHexa(n); return 0; } |
JAVA
// Java program to convert a decimal // number to hexadecimal number import java.io.*; class GFG { // function to convert decimal to hexadecimal static void decToHexa( int n) { // char array to store hexadecimal number char [] hexaDeciNum = new char [ 100 ]; // counter for hexadecimal number array int i = 0 ; while (n != 0 ) { // temporary variable to store remainder int temp = 0 ; // storing remainder in temp variable. temp = n % 16 ; // check if temp < 10 if (temp < 10 ) { hexaDeciNum[i] = ( char )(temp + 48 ); i++; } else { hexaDeciNum[i] = ( char )(temp + 55 ); i++; } n = n / 16 ; } // printing hexadecimal number array in reverse // order for ( int j = i - 1 ; j >= 0 ; j--) System.out.print(hexaDeciNum[j]); } // driver program public static void main(String[] args) { int n = 2545 ; decToHexa(n); } } // Contributed by Pramod Kumar |
Python3
# Python3 program to # convert a decimal # number to hexadecimal # number # function to convert # decimal to hexadecimal def decToHexa(n): # char array to store # hexadecimal number hexaDeciNum = [ '0' ] * 100 # counter for hexadecimal # number array i = 0 while (n ! = 0 ): # temporary variable # to store remainder temp = 0 # storing remainder # in temp variable. temp = n % 16 # check if temp < 10 if (temp < 10 ): hexaDeciNum[i] = chr (temp + 48 ) i = i + 1 else : hexaDeciNum[i] = chr (temp + 55 ) i = i + 1 n = int (n / 16 ) # printing hexadecimal number # array in reverse order j = i - 1 while (j > = 0 ): print ((hexaDeciNum[j]), end = "") j = j - 1 # Driver Code n = 2545 decToHexa(n) # This code is contributed # by mits. |
C#
// C# program to convert a decimal // number to hexadecimal number using System; class GFG { // function to convert decimal // to hexadecimal static void decToHexa( int n) { // char array to store // hexadecimal number char [] hexaDeciNum = new char [100]; // counter for hexadecimal number array int i = 0; while (n != 0) { // temporary variable to // store remainder int temp = 0; // storing remainder in temp // variable. temp = n % 16; // check if temp < 10 if (temp < 10) { hexaDeciNum[i] = ( char )(temp + 48); i++; } else { hexaDeciNum[i] = ( char )(temp + 55); i++; } n = n / 16; } // printing hexadecimal number // array in reverse order for ( int j = i - 1; j >= 0; j--) Console.Write(hexaDeciNum[j]); } // Driver Code public static void Main(String[] args) { int n = 2545; decToHexa(n); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to convert // a decimal number to // hexadecimal number // function to convert // decimal to hexadecimal function decToHexa( $n ) { // char array to store // hexadecimal number $hexaDeciNum ; // counter for hexadecimal // number array $i = 0; while ( $n != 0) { // temporary variable // to store remainder $temp = 0; // storing remainder // in temp variable. $temp = $n % 16; // check if temp < 10 if ( $temp < 10) { $hexaDeciNum [ $i ] = chr ( $temp + 48); $i ++; } else { $hexaDeciNum [ $i ] = chr ( $temp + 55); $i ++; } $n = (int)( $n / 16); } // printing hexadecimal number // array in reverse order for ( $j = $i - 1; $j >= 0; $j --) echo $hexaDeciNum [ $j ]; } // Driver Code $n = 2545; decToHexa( $n ); // This code is contributed // by mits. ?> |
Javascript
<script> // Javascript program to convert a decimal // number to hexadecimal number // function to convert decimal to hexadecimal function decToHexa(n) { // char array to store hexadecimal number var hexaDeciNum = Array.from({length: 100}, (_, i) => 0); // counter for hexadecimal number array var i = 0; while (n != 0) { // temporary variable to store remainder var temp = 0; // storing remainder in temp variable. temp = n % 16; // check if temp < 10 if (temp < 10) { hexaDeciNum[i] = String.fromCharCode(temp + 48); i++; } else { hexaDeciNum[i] = String.fromCharCode(temp + 55); i++; } n = parseInt(n / 16); } // printing hexadecimal number array in reverse // order for (j = i - 1; j >= 0; j--) document.write(hexaDeciNum[j]); } // driver program var n = 2545; decToHexa(n); // This code contributed by shikhasingrajput </script> |
9F1
使用预定义函数
JAVA
// Java program to convert a decimal // number to hexadecimal number import java.io.*; class GFG { public static void decToHexa( int n) { System.out.println(Integer.toHexString(n)); } public static void main(String[] args) { int n = 2545 ; decToHexa(n); } } |
Python3
# Python program to convert a decimal # number to hexadecimal number # function to convert decimal number # to equivalent hexadecimal number def decToHexa(n): return hex (n).replace( "0x" ,"") # Driver Code n = 2545 print (decToHexa(n)) # This code is contributed by shahidedu7. |
C#
// C# program to convert a decimal // number to hexadecimal number using System; class GFG { public static void decToHexa( int n) { Console.Write(Convert.ToString(n)); } public static void Main(String[] args) { int n = 2545; decToHexa(n); } } // This code is contributed by shivanisinghss2110 |
9f1
本文由 严酷的阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。