背景:在linux需要将网络字节序的unicode编码与字符串相互转换。研究一段时间粗略的写了俩个接口,字节序方面需要根据需要自行处理。因为用的是C语言的标准库,应当是可以跨平台应用的(未验证)。
C语言代码
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <wchar.h>
#include <locale.h>
void hex_data_to_log_data(unsigned char *src, int len, char *dst)
{
int i = 0;
int index = 0;
char buff[1024] = {0};
for (i = 0; i < len; i++) {
index += sprintf(buff + index, "%02x", src[i]);
if (i != len - 1)
index += sprintf(buff + index, " ");
}
if (strlen(buff) > 0)
strcpy(dst, buff);
}
void print_hex_data(unsigned char *modbus_cmd, int len, char *title)
{
char log_modbus_data[1024] = {0};
int size = len;
int offset = 0;
int print_len = 0;
int count = 0;
int max_len = 60;
if (size <= max_len) {
hex_data_to_log_data(modbus_cmd, size, log_modbus_data);
if (strlen(log_modbus_data) > 0)
printf("%s:%s", title, log_modbus_data);
} else {
while (size > 0) {
count++;
if (size > max_len)
print_len = max_len;
else
print_len = size;
hex_data_to_log_data(modbus_cmd + offset, print_len, log_modbus_data);
if (strlen(log_modbus_data) > 0)
printf("%s-%d:%s", title, count, log_modbus_data);
offset += print_len;
size -= print_len;
}
}
}
int asc_to_unicode(char * in, unsigned char * out) {
wchar_t wstr[256] = {0};
int index = 0;
int i = 0;
setlocale(LC_ALL, "");
mbstowcs(wstr, in, 256);
for (i = 0; i < wcslen(wstr); i++) {
memcpy(out + index, &wstr[i], 2);
index += 2;
}
print_hex_data(out, index, "asc_to_unicode");
return index;
}
int unicode_to_asc(unsigned char *in, int in_len,char *out) {
wchar_t wstr[256] = {0};
unsigned short tmp_short;
int index_cnt = 0;
int j = 0;
for(j = 0; j < in_len; j+=2) {
memcpy(&tmp_short, in + j, 2);
wstr[index_cnt++] = tmp_short;
}
wcstombs(out, wstr, 256);
printf("\nunicode_to_asc:%s\n", out);
return strlen(out);
}
int main()
{
char asc[256] = {0};
unsigned char hex[512] = {0};
asc_to_unicode("欢迎评论交流", hex);
unicode_to_asc(hex, 12, asc);
return(0);
}
验证结果
C语言在线运行以上代码输出。

在线中文unicode转换网站验证(字节序方面自行处理)
