이 튜토리얼에서는 문자열을 16진수 ASCII 값으로 변환하는 프로그램에 대해 설명합니다.
이를 위해 문자열이 제공됩니다. 우리의 임무는 주어진 특정 문자열을 해당하는 16진수로 인쇄하는 것입니다.
예시
#include <stdio.h>
#include <string.h>
//converting string to hexadecimal
void convert_hexa(char* input, char* output){
int loop=0;
int i=0;
while(input[loop] != '\0'){
sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
i+=2;
}
//marking the end of the string
output[i++] = '\0';
}
int main(){
char ascii_str[] = "tutorials point";
int len = strlen(ascii_str);
char hex_str[(len*2)+1];
//function call
convert_hexa(ascii_str, hex_str);
printf("ASCII: %s\n", ascii_str);
printf("Hexadecimal: %s\n", hex_str);
return 0;
} 출력
ASCII: tutorials point Hexadecimal: 7475746F7269616C7320706F696E74