在 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