[英](Only C) conversion special characters from string char to Hex. Unicode
我正在嘗試使用此代碼: https : //www.includehelp.com/c/convert-ascii-string-to-hexadecimal-string-in-c.aspx
這段代碼在我的程序上完美運行。
它可以完美地從 utf-8 轉換為十六進制 unicode 字符,如 A,m,n,d,0,9。
拜托,任何人都可以告訴我或修改這個程序,當在字符串中我們有“特殊字符”時,比如帶有重音的人聲(ñ、ç、à、á、...)。 因為,當我運行這個程序時,它不像我預期的那樣工作。
我正在使用本機 C 的 RHEL 7(抱歉,我不知道版本)
我試圖轉換為十六進制Unicode的特殊字符是 UTF-8。
#include <stdio.h>
#include <string.h>
//function to convert ascii char[] to hex-string (char[])
void string2hexString(char* input, char* output)
{
int loop;
int i;
i=0;
loop=0;
while(input[loop] != '\0')
{
sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
i+=2;
}
//insert NULL at the end of the output string
output[i++] = '\0';
}
int main(){
char ascii_str[] = "Hello world!";
//declare output string with double size of input string
//because each character of input string will be converted
//in 2 bytes
int len = strlen(ascii_str);
char hex_str[(len*2)+1];
//converting ascii string to hex string
string2hexString(ascii_str, hex_str);
printf("ascii_str: %s\n", ascii_str);
printf("hex_str: %s\n", hex_str);
return 0;
}
輸出
ascii_str: Hello world!
hex_str: 48656C6C6F20776F726C6421
我想要像“áéíóúàèìòùç”這樣的條目“ascii_str”,並且能夠在字符串上獲得這個十六進制 UNIcodes 作為這個代碼:( unicode )
letra-> á // cod.hex--> e1
letra-> é // cod.hex--> e9
letra-> í // cod.hex--> ed
letra-> ó // cod.hex--> f3
letra-> ú // cod.hex--> fa
letra-> à // cod.hex--> e0
letra-> è // cod.hex--> e8
letra-> ì // cod.hex--> ec
letra-> ò // cod.hex--> f2
letra-> ù // cod.hex--> f9
letra-> ç // cod.hex--> e7
請閱讀有關 utf-8 編碼的信息。 UTF-8 編碼具有可變長度的代碼點(字符)。 您正在轉換的符號占用兩個字節,而不是一個。 因此,您使用的代碼不適合此目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.