Donada la cadena de valor hexadecimal com a entrada, la tasca és convertir la cadena de valor hexadecimal donada a la cadena de format ASCII corresponent.
Exemples:
Entrada: 6765656b73
Sortida: frikisEntrada: 6176656e67657273
Sortida: venjadors
El sistema de numeració hexadecimal o simplement hexadecimal utilitza el sistema Base de 16. En ser un sistema Base-16, hi ha 16 símbols de dígits possibles. El nombre hexadecimal utilitza 16 símbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} per representar tots els nombres. Aquí, (A, B, C, D, E, F) representa (10, 11, 12, 13, 14, 15).
ASCII significa Codi estàndard americà per a l'intercanvi d'informació . ASCII és un estàndard que assigna lletres, números i altres caràcters dins de les 256 ranures disponibles en el codi de 8 bits. Per exemple, el caràcter h minúscula (Char) té un valor decimal de 104, que és 01101000 en binari i 68 en hexadecimal.
Algorisme:
- Inicialitzar la cadena ascii final com a buida.
- Extreu els dos primers caràcters de la cadena hexadecimal presa com a entrada.
- Converteix-lo en nombre enter de base 16.
- Emet aquest nombre enter al caràcter que és l'equivalent ASCII de 2 caràcters hexadecimals.
- Afegiu aquest caràcter a la cadena final.
- Extreu els dos caràcters següents de la cadena hexadecimal i aneu al pas 3.
- Seguiu aquests passos per extreure tots els caràcters de la cadena hexadecimal.

Implementació:
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> |
>
descarregar vídeos de youtube a vlc
>
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')); } }> |
>
>
Python 3
# 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#
conversió de cadena a enter en java
// 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'));> |
>
>Sortida
geeks>
Complexitat temporal : O(N), on N és la longitud de la cadena donada
Espai auxiliar : O(N)
Enfocament 2: Ús d'operacions per bits:
Aquest enfocament consisteix a utilitzar operacions bit a bit per convertir directament la cadena hexadecimal en una cadena ASCII. En aquest enfocament, començaríem convertint la cadena hexadecimal en una sèrie de bytes. Ho podem fer iterant per la cadena i convertint cada parell de dígits hexadecimals en un byte. Un cop tenim els bytes, podem utilitzar operacions bit a bit per convertir-los en caràcters de la cadena ASCII.
En aquesta implementació, utilitzem un stringstream per construir la cadena ASCII. Iterem per la cadena hexadecimal, convertint cada parell de dígits hexadecimals en un byte mitjançant stoi. A continuació, afegim el byte al flux de cadena. Finalment, retornem el contingut del flux de cadena com a cadena ASCII.
Aquí teniu el codi d'aquest enfocament:
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
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); } }> |
>
>
com convertir una cadena a enter
Python 3
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.> |
fons css
>
>
Sortida
geeks>
Complexitat temporal: O(n), on N és la longitud de la cadena donada
Espai auxiliar: O(n)