logo

16 進値文字列を ASCII 値文字列に変換する

入力として 16 進値文字列を指定すると、タスクは、指定された 16 進値文字列を対応する ASCII 形式文字列に変換することです。

例:

入力: 6765656b73
出力: オタク



入力: 6176656e67657273
出力: アベンジャーズ

16 進数または単に 16 進数の番号付けシステムでは、16 進数システムが使用されます。 Base-16 システムであるため、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 は、8 ビット コードで使用可能な 256 スロット内に文字、数字、およびその他の文字を割り当てる規格です。たとえば、小文字の h 文字 (Char) の 10 進値は 104 で、2 進数では 01101000、16 進数では 68 になります。

アルゴリズム:

  1. 最終的な ASCII 文字列を空として初期化します。
  2. 入力として受け取った 16 進文字列から最初の 2 文字を抽出します。
  3. それを基数 16 の整数に変換します。
  4. この整数を、16 進数 2 文字に相当する ASCII 文字にキャストします。
  5. この文字を最終文字列に追加します。
  6. 16 進数の文字列から次の 2 文字を抽出し、手順 3 に進みます。
  7. 16 進文字列からすべての文字を抽出するには、次の手順に従います。

実装:

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 { // 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 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 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




Java文字列の書式設定
# 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 { // 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




> >// JavaScript program to convert hexadecimal> >// string to ASCII format string> >function> hexToASCII(hex) {> >// initialize the ASCII code string as empty.> >var> ascii =>''>;> >for> (>var> i = 0; i // extract two characters from hex string var part = hex.substring(i, i + 2); // change it into base 16 and // typecast as the character var ch = String.fromCharCode(parseInt(part, 16)); // add this char to final ASCII string ascii = ascii + ch; } return ascii; } // Driver Code // print the ASCII string. document.write(hexToASCII('6765656b73'));>

>

>

出力

geeks>

時間計算量 : O(N)、N は指定された文字列の長さです
補助スペース : の上)

アプローチ 2: ビット単位の演算を使用する:

このアプローチは、ビット単位の演算を使用して 16 進文字列を ASCII 文字列に直接変換することです。このアプローチでは、16 進文字列を一連のバイトに変換することから始めます。これを行うには、文字列を反復処理し、16 進数の各ペアをバイトに変換します。バイトを取得したら、ビット単位の演算を使用して、バイトを ASCII 文字列の文字に変換できます。

この実装では、stringstream を使用して ASCII 文字列を構築します。 16 進数文字列を反復処理し、stoi を使用して 16 進数の各ペアをバイトに変換します。次に、そのバイトを文字列ストリームに追加します。最後に、stringstream の内容を ASCII 文字列として返します。

このアプローチのコードは次のとおりです。

C++




#include> using> namespace> std;> string hexToASCII(std::string hex) {> >stringstream ss;> >for> (>size_t> i = 0; i unsigned char byte =stoi(hex.substr(i, 2), nullptr, 16); ss << byte; } return ss.str(); } int main() { string hexString = '6765656b73'; string asciiString = hexToASCII(hexString); cout << asciiString << endl; return 0; }>

>

>

ジャワ

最も近いJavaScript




import> java.util.*;> public> class> HexToASCII {> >public> static> String hexToASCII(String hex) {> >StringBuilder sb =>new> StringBuilder();> >for> (>int> i =>0>; i 2) { String str = hex.substring(i, i + 2); char ch = (char) Integer.parseInt(str, 16); sb.append(ch); } return sb.toString(); } public static void main(String[] args) { String hexString = '6765656b73'; String asciiString = hexToASCII(hexString); System.out.println(asciiString); } }>

>

>

Python3




def> hex_to_ascii(hex_str):> >ascii_str>=> ''> >for> i>in> range>(>0>,>len>(hex_str),>2>):> >byte>=> int>(hex_str[i:i>+>2>],>16>)> >ascii_str>+>=> chr>(byte)> >return> ascii_str> # Driver code> hex_string>=> '6765656b73'> ascii_string>=> hex_to_ascii(hex_string)> print>(ascii_string)>

>

>

C#




using> System;> using> System.Text;> public> class> Program> {> >public> static> string> HexToASCII(>string> hex)> >{> >StringBuilder sb =>new> StringBuilder();> >for> (>int> i = 0; i { byte b = Convert.ToByte(hex.Substring(i, 2), 16); sb.Append((char)b); } return sb.ToString(); } public static void Main() { string hexString = '6765656b73'; string asciiString = HexToASCII(hexString); Console.WriteLine(asciiString); } } // This code is contributed by Prajwal Kandekar>

魅力的なコンピューター言語
>

>

JavaScript




// Javascript code addition> function> hexToASCII(hex) {> >let sb =>''>;> >for> (let i = 0; i let str = hex.substring(i, i + 2); let ch = String.fromCharCode(parseInt(str, 16)); sb += ch; } return sb; } let hexString = '6765656b73'; let asciiString = hexToASCII(hexString); console.log(asciiString); // The code is contributed by Nidhi goel.>

>

>

出力

geeks>

時間計算量: O(n)、N は指定された文字列の長さです
補助スペース:O(n)