在 C++ 中将字符串转换为十六进制 ASCII 值
在本教程中,我们将讨论一个程序,该程序将字符串转换为十六进制 ASCII 值。
为此,我们将提供一个字符串字符。我们的任务是将给定的特定字符串打印为其十六进制等效值。
示例
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP