在C ++中将字符串转换为十六进制ASCII值
在本教程中,我们将讨论将字符串转换为十六进制ASCII值的程序。
为此,我们将提供一个字符串。我们的任务是将特定的给定字符串打印为等效的十六进制。
示例
#include <stdio.h>
#include <string.h>
//将字符串转换为十六进制
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;
}
//标记字符串的结尾
output[i++] = '\0';
}
int main(){
char ascii_str[] = "nhooo.com";
int len = strlen(ascii_str);
char hex_str[(len*2)+1];
//函数调用
convert_hexa(ascii_str, hex_str);
printf("ASCII: %s\n", ascii_str);
printf("Hexadecimal: %s\n", hex_str);
return 0;
}输出结果
ASCII: nhooo.com Hexadecimal: 7475746F7269616C7320706F696E74
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短