给定一个二进制数,任务是将该二进制数转换为其等效的十六进制十进制数。
例子:
Input: 100000101111
Output: 82F
Explanation:
Dividing the number into chunks of 4, it becomes 1000 0010 1111.
Here, 1000 is equivalent to 8,
0010 is equivalent to 2 and
1111 is equivalent to F
Therefore, the number becomes 82F
Input:10101101
Output:AD
Explanation:
Dividing the number into chunks of 4, it becomes 1010 1101.
Here, 1010 is equivalent to A and
1101 is equivalent to D.
Therefore, the number becomes AD.
方法:
- 将给定的二进制数划分为4的块,然后开始计算其等效的HexaDecimal形式。
- 将此数字存储在向量中。
- 对给定二进制数的所有数字重复该过程。
- 以相反的顺序打印存储在向量中的数字。
下面是上述方法的实现:
C++
// C++ code to convert Binary to its
// HexaDecimal number(base 16).
// Including Header Files
#include
using namespace std;
// Function to convert
// Binary to HexaDecimal
void bToHexaDecimal(string s)
{
int len = s.length(), check = 0;
int num = 0, sum = 0, mul = 1;
vector ans;
// Iterating through
// the bits backwards
for (int i = len - 1; i >= 0; i--) {
sum += (s[i] - '0') * mul;
mul *= 2;
check++;
// Computing the HexaDecimal
// Number formed so far
// and storing it in a vector.
if (check == 4 || i == 0) {
if (sum <= 9)
ans.push_back(sum + '0');
else
ans.push_back(sum + 55);
// Reinitializing all
// variables for next group.
check = 0;
sum = 0;
mul = 1;
}
}
len = ans.size();
// Printing the Hexadecimal
// number formed so far.
for (int i = len - 1; i >= 0; i--)
cout << ans[i];
}
// Driver Code
int main()
{
string s = "100000101111";
// Function Call
bToHexaDecimal(s);
return 0;
}
Java
// Java code to convert BCD to its
// HexaDecimal number(base 16).
// Including Header Files
import java.util.*;
class GFG{
// Function to convert
// BCD to HexaDecimal
static void bcdToHexaDecimal(char []s)
{
int len = s.length, check = 0;
int num = 0, sum = 0, mul = 1;
Vector ans =
new Vector();
// Iterating through
// the bits backwards
for (int i = len - 1; i >= 0; i--)
{
sum += (s[i] - '0') * mul;
mul *= 2;
check++;
// Computing the HexaDecimal
// Number formed so far
// and storing it in a vector.
if (check == 4 || i == 0)
{
if (sum <= 9)
ans.add((char) (sum + '0'));
else
ans.add((char) (sum + 55));
// Reinitializing all
// variables for next group.
check = 0;
sum = 0;
mul = 1;
}
}
len = ans.size();
// Printing the Hexadecimal
// number formed so far.
for (int i = len - 1; i >= 0; i--)
System.out.print(ans.get(i));
}
// Driver Code
public static void main(String[] args)
{
String s = "100000101111";
// Function Call
bcdToHexaDecimal(s.toCharArray());
}
}
// This code is contributed by Princi Singh
Python3
# Python3 code to convert BCD to its
# hexadecimal number(base 16).
# Function to convert BCD to hexadecimal
def bcdToHexaDecimal(s):
len1 = len(s)
check = 0
num = 0
sum = 0
mul = 1
ans = []
# Iterating through the bits backwards
i = len1 - 1
while(i >= 0):
sum += (ord(s[i]) - ord('0')) * mul
mul *= 2
check += 1
# Computing the hexadecimal number formed
# so far and storing it in a vector.
if (check == 4 or i == 0):
if (sum <= 9):
ans.append(chr(sum + ord('0')))
else:
ans.append(chr(sum + 55));
# Reinitializing all variables
# for next group.
check = 0
sum = 0
mul = 1
i -= 1
len1 = len(ans)
# Printing the hexadecimal
# number formed so far.
i = len1 - 1
while(i >= 0):
print(ans[i], end = "")
i -= 1
# Driver Code
if __name__ == '__main__':
s = "100000101111"
# Function Call
bcdToHexaDecimal(s)
# This code is contributed by Samarth
C#
// C# code to convert BCD to its
// HexaDecimal number(base 16).
// Including Header Files
using System;
using System.Collections.Generic;
class GFG{
// Function to convert
// BCD to HexaDecimal
static void bcdToHexaDecimal(char []s)
{
int len = s.Length, check = 0;
int num = 0, sum = 0, mul = 1;
List ans =
new List();
// Iterating through
// the bits backwards
for (int i = len - 1; i >= 0; i--)
{
sum += (s[i] - '0') * mul;
mul *= 2;
check++;
// Computing the HexaDecimal
// Number formed so far
// and storing it in a vector.
if (check == 4 || i == 0)
{
if (sum <= 9)
ans.Add((char) (sum + '0'));
else
ans.Add((char) (sum + 55));
// Reinitializing all
// variables for next group.
check = 0;
sum = 0;
mul = 1;
}
}
len = ans.Count;
// Printing the Hexadecimal
// number formed so far.
for (int i = len - 1; i >= 0; i--)
Console.Write(ans[i]);
}
// Driver Code
public static void Main(String[] args)
{
String s = "100000101111";
// Function Call
bcdToHexaDecimal(s.ToCharArray());
}
}
// This code is contributed by 29AjayKumar
输出:
82F