logo

Convertir une chaîne de valeur hexadécimale en chaîne de valeur ASCII

Étant donné la chaîne de valeur hexadécimale en entrée, la tâche consiste à convertir la chaîne de valeur hexadécimale donnée en sa chaîne de format ASCII correspondante.

Exemples:

Saisir: 6765656b73
Sortir: les geeks



Saisir: 6176656e67657273
Sortir: vengeurs

Le système de numérotation hexadécimal ou simplement hexadécimal utilise le système Base 16. Étant un système Base-16, il existe 16 symboles numériques possibles. Le nombre hexadécimal utilise 16 symboles {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} pour représenter tous les nombres. Ici, (A, B, C, D, E, F) représente (10, 11, 12, 13, 14, 15).

ASCII signifie Code américain normalisé pour l'échange d'information . ASCII est une norme qui attribue des lettres, des chiffres et d'autres caractères dans les 256 emplacements disponibles dans le code 8 bits. Par exemple, le caractère h minuscule (Char) a une valeur décimale de 104, soit 01101000 en binaire et 68 en hexadécimal.

Algorithme:

  1. Initialisez la chaîne ascii finale comme étant vide.
  2. Extrayez les deux premiers caractères de la chaîne hexadécimale prise en entrée.
  3. Convertissez-le en entier en base 16.
  4. Convertissez cet entier en un caractère qui est l'équivalent ASCII de 2 caractères hexadécimaux.
  5. Ajoutez ce caractère à la chaîne finale.
  6. Extrayez les deux caractères suivants de la chaîne hexadécimale et passez à l'étape 3.
  7. Suivez ces étapes pour extraire tous les caractères de la chaîne hexadécimale.

Mise en œuvre:

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>

>

pas nul en js
>

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

>

acteur Govinda

>

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'));>

>

>

Sortir

geeks>

Complexité temporelle : O(N), où N est la longueur de la chaîne donnée
Espace auxiliaire : SUR)

Approche 2 : Utilisation des opérations au niveau du bit :

Cette approche consiste à utiliser des opérations au niveau du bit pour convertir directement la chaîne hexadécimale en chaîne ASCII. Dans cette approche, nous commencerions par convertir la chaîne hexadécimale en une série d’octets. Nous pouvons le faire en parcourant la chaîne et en convertissant chaque paire de chiffres hexadécimaux en octet. Une fois que nous avons les octets, nous pouvons utiliser des opérations au niveau du bit pour les convertir en caractères de la chaîne ASCII.

Dans cette implémentation, nous utilisons un stringstream pour construire la chaîne ASCII. Nous parcourons la chaîne hexadécimale, convertissant chaque paire de chiffres hexadécimaux en un octet en utilisant stoi. Ensuite, nous ajoutons l’octet au stringstream. Enfin, nous renvoyons le contenu du stringstream sous forme de chaîne ASCII.

Voici le code de cette approche :

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; }>

>

>

Java




inversion de chaîne en c

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>

>

>

types d'arbres binaires

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.>

>

>

Sortir

geeks>

Complexité temporelle : O(n), où N est la longueur de la chaîne donnée
Espace auxiliaire : O(n)