将十六进制值字符串转换为 ASCII 值字符串
给定十六进制值字符串作为输入,任务是将给定的十六进制值字符串转换为其对应的 ASCII 格式字符串。
例子:
Input: 6765656b73
Output: geeks
Input: 6176656e67657273
Output: avengers
“十六进制”或简称“十六进制”编号系统使用以 16 为基数的系统。作为 Base-16 系统,有 16 个可能的数字符号。十六进制数使用 16 个符号 {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} 来表示所有数字。这里,(A、B、C、D、E、F)表示(10、11、12、13、14、15)。
ASCII 代表美国信息交换标准代码。 ASCII 是一种在 8 位代码中可用的 256 个插槽内分配字母、数字和其他字符的标准。例如,小写“h”字符(Char)的十进制值为 104,即二进制的“01101000”和十六进制的“68”。
有关更多 ASCII 值,请参阅 ASCII TABLE。
Algorithm:
- Initialize final ascii string as empty.
- Extract first two characters from the hexadecimal string taken as input.
- Convert it into base 16 integer.
- Cast this integer to character which is ASCII equivalent of 2 char hex.
- Add this character to final string.
- Extract next two characters from hexadecimal string and go to step 3.
- Follow these steps to extract all characters from hexadecimal string.
C++
// C++ program to convert hexadecimal
// string to ASCII format string
#include
using namespace std;
string hexToASCII(string hex)
{
// initialize the ASCII code string as empty.
string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2)
{
// extract two characters from hex string
string part = hex.substr(i, 2);
// change it into base 16 and
// typecast as the character
char ch = stoul(part, nullptr, 16);
// add this char to final ASCII string
ascii += ch;
}
return ascii;
}
// Driver Code
int main()
{
// print the ASCII string.
cout << hexToASCII("6765656b73") << endl;
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java program to convert hexadecimal
// string to ASCII format string
import java.util.Scanner;
public class HexadecimalToASCII {
public static String hexToASCII(String hex)
{
// initialize the ASCII code string as empty.
String ascii = "";
for (int i = 0; i < hex.length(); i += 2) {
// extract two characters from hex string
String part = hex.substring(i, i + 2);
// change it into base 16 and typecast as the character
char ch = (char)Integer.parseInt(part, 16);
// add this char to final ASCII string
ascii = ascii + ch;
}
return ascii;
}
public static void main(String[] args)
{
// print the ASCII string.
System.out.println(hexToASCII("6765656b73"));
}
}
Python3
# Python3 program to convert hexadecimal
# string to ASCII format string
def hexToASCII(hexx):
# initialize the ASCII code string as empty.
ascii = ""
for i in range(0, len(hexx), 2):
# extract two characters from hex string
part = hexx[i : i + 2]
# change it into base 16 and
# typecast as the character
ch = chr(int(part, 16))
# add this char to final ASCII string
ascii += ch
return ascii
# Driver Code
if __name__ == "__main__":
# print the ASCII string.
print(hexToASCII("6765656b73"))
# This code is contributed by
# sanjeev2552
C#
// C# program to convert hexadecimal
// string to ASCII format string
using System;
class GFG
{
public static String hexToASCII(String hex)
{
// initialize the ASCII code string as empty.
String ascii = "";
for (int i = 0; i < hex.Length; i += 2)
{
// extract two characters from hex string
String part = hex.Substring(i, 2);
// change it into base 16 and
// typecast as the character
char ch = (char)Convert.ToInt32(part, 16);;
// add this char to final ASCII string
ascii = ascii + ch;
}
return ascii;
}
// Driver Code
public static void Main(String[] args)
{
// print the ASCII string.
Console.WriteLine(hexToASCII("6765656b73"));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
geeks