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는 다음을 의미합니다. 정보 교환을 위한 미국 표준 코드 . ASCII는 8비트 코드에서 사용 가능한 256개 슬롯 내에서 문자, 숫자 및 기타 문자를 할당하는 표준입니다. 예를 들어 소문자 h 문자(Char)의 10진수 값은 104이며, 이는 2진수에서는 01101000이고 16진수에서는 68입니다.

연산:

  1. 최종 ASCII 문자열을 빈 상태로 초기화합니다.
  2. 입력으로 사용된 16진수 문자열에서 처음 두 문자를 추출합니다.
  3. 이를 기본 16 정수로 변환합니다.
  4. 이 정수를 2문자 16진수에 해당하는 ASCII 문자로 변환합니다.
  5. 이 문자를 최종 문자열에 추가하세요.
  6. 16진수 문자열에서 다음 두 문자를 추출하고 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')); } }>

>

>

파이썬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# 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 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 문자열의 문자로 변환할 수 있습니다.

이 구현에서는 문자열 스트림을 사용하여 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; }>

>

>

자바




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

>

번호가 매겨진 알파벳
>

파이썬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)>

>

>

씨#




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>

>

>

자바스크립트


C의 배열에 있는 문자열



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