C语言 字符串与unicode编码互转

背景:在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转换网站验证(字节序方面自行处理)

网站在线转换结果

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值