How to convert a unicode character array back to unicode sequence in C++
My problem is how to convert a c/c++ string/chractor array to another string contain the unicode(UTF-16) escape sequence of original one
for example, I want开发者_开发知识库 to find a function F(char *ch) could do following function.
char a[10] = "\u5f53";
printf("a = %s\n",a);
char b[10];
b = F(a); //<- F is the function I wanted
printf("b = %s\n",b);
-------- console will show -------
a = 張
b = \u5f53
Anyone has any Idea@@?~
thanks!!
ps: I tried to guess \u5f35 means the value store in a, but it is not indeed the value of a[0] = -79 , a[1] = 105 ... So I don't know how to convert it back to the sequence of unicode.... Please give me a hane~ : )
Here is an example that uses the iconv
library.
#include <stdio.h>
#include <string.h>
#include <iconv.h>
void F(char *a, char *b, size_t bufsize) {
size_t in_len = strlen(a);
size_t out_len = bufsize - 1;
iconv_t cd = iconv_open("C99", "UTF-8");
iconv(cd, &a, &in_len, &b, &out_len);
*b = 0;
}
int main(void) {
char a[10] = "\u5f53";
printf("a = %s\n",a);
char b[10];
F(a, b, 10); //<- F is the function I wanted
printf("b = %s\n",b);
return 0;
}
Have a look at _mbstowcs_s_l
I am not sure of how exactly the unicode point needs to be fed to the function... perhaps like you have ("\uxxxx") or maybe "&#xxxx"
精彩评论