嵌入式linux无法显示中文问题

1. 前言

       网上关于嵌入式linux无法显示中文问题的文章很多,但又没有彻底的解决我的问题,甚是纠结啊...具体不支持的原因是busybox1.17.0之后的版本,shell命令行对中文输入及显示做了强制限制,所以即使内核设置了对中文的支持,但在shell下依然无法显示中文。

2. 问题现象

挂载U盘后,U盘内部为中文的目录和文件分别显示?????


针对此种问题需修改如下:

a. 去掉文件系统对中文的限制
b. 添加内核对中文字符编码的支持(本文档以UTF-8为例)
c. 应用程序选择合适的字符编码(本文档以串口终端软件设置为例)

3. 问题修改

3.1 设置CRT终端为UTF8格式


3.2 busybox修改

      本文档以busybox-1.22.1为例,描述如何修改busybox源码。

3.2.1 修改busybox-1.22.1/libbb/printable_string.c

const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str)
{
	char *dst;
	const char *s;

	s = str;
	while (1) {
		unsigned char c = *s;
		if (c == '\0') {
			/* 99+% of inputs do not need conversion */
			if (stats) {
				stats->byte_count = (s - str);
				stats->unicode_count = (s - str);
				stats->unicode_width = (s - str);
			}
			return str;
		}
		if (c < ' ')
			break;
		//屏蔽以下语句
		/*
		if (c >= 0x7f)
			break;
		*/
		s++;
	}

#if ENABLE_UNICODE_SUPPORT
	dst = unicode_conv_to_printable(stats, str);
#else
	{
		char *d = dst = xstrdup(str);
		while (1) {
			unsigned char c = *d;
			if (c == '\0')
				break;
			//修改如下代码
			//if (c < ' ' || c >= 0x7f)
			if (c < ' ')	
				*d = '?';
			d++;
		}
		if (stats) {
			stats->byte_count = (d - dst);
			stats->unicode_count = (d - dst);
			stats->unicode_width = (d - dst);
		}
	}
#endif
	return auto_string(dst);
}

修改20~24,37~38行代码。

3.2.2 修改busybox-1.22.1/libbb/unicode.c

static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags) 
{          
	char *dst;         
	unsigned dst_len;         
	unsigned uni_count;         
	unsigned uni_width;           

	if (unicode_status != UNICODE_ON) {                 
		char *d;                  
		if (flags & UNI_FLAG_PAD) {                         
			d = dst = xmalloc(width + 1);                         
			while ((int)--width >= 0) {                                  
				unsigned char c = *src;                                 
				if (c == '\0') {                                         
					do                                                  
						*d++ = ' ';                                          
					while ((int)--width >= 0);                                         
					break;                                 
				}               
				
				/* 将下行代码行修改为:*d++ = (c >= ' ') ? c : '?'; */                                  
				*d++ = (c >= ' ' && c < 0x7f) ? c : '?';                                  
				src++;                         
			}                         
			*d = '\0';                 
		} else {                          
			d = dst = xstrndup(src, width);                         
			while (*d) {                                  
				unsigned char c = *d;               
				/* 将下行代码行修改为:if (c < ' ' )  */                                  
				if (c < ' ' || c >= 0x7f)                                         
				*d = '?';                                  
				d++;                         
			}                 
		}                 
		if (stats) {                          
			stats->byte_count = (d - dst);                         
			stats->unicode_count = (d - dst);                         
			stats->unicode_width = (d - dst);                 
		}                 
	}
	return dst;         
}		

3.2.3 busybox配置

Busybox Settings  --->  
	General Configuration  --->   
	[*] Support Unicode (NEW)   
	[*]   Check $LC_ALL, $LC_CTYPE and $LANG environment variables

3.3 linux内核配置iocharset

File systems  --->  
	-*- Native language support  --->   
		...
		<*>   Simplified Chinese charset (cp936, GB2312)	
		...
		<*>   NLS UTF-8

3.4 挂载U盘显示

以下根据不同的环境使用

utf8格式挂载:

mount -t vfat -o iocharset=utf8 /dev/sda1 /mnt/usb

gb2312格式挂载:

mount -t vfat -o iocharset=cp936 /dev/sda1 /mnt/usb

注:-o iocharset=utf8编码格式的支持



百度文库里有一篇“Linux”,具体路径:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值