|
发表于 2013-12-17 08:54:34
|
显示全部楼层
这几天也碰到了一个unicode问题,不过幸运的是解决了,首先是fatfs unicode的使用问题,在ffconf.h中设置#define _LFN_UNICODE 1
然后之后每次打开文件或者目录都要换一种形式比如res = f_open(&fontfsrc, _T("0:/sys/yahei24.xbf"), FA_OPEN_EXISTING | FA_READ);多了一个_T()宏,其实直接写L也可以,
然后如果路径是存成string的话要怎么办呢?当时我c语言基本功不好折腾了几个小时:
添加#include <wchar.h>
TCHAR _filepath[100];
char FilePath[96];
sprintf(FilePath,"%s","0:/user");
swprintf(_filepath,sizeof(_filepath),L"%s",filename);
这样_filepath的内容就变成L""0:/user" 啦,可以直接使用f_opendir(&DirInf,_filepath)打开路径啦
最后是读取文件名的问题,fatfs使用的是UTF16格式,但是有些需要用到UTF8(比如emwin)所以还写了一个转码函数
int unicode_to_utf8(uint16_t *in, char *tmp)
{
int i = 0;
int outsize = 0;
int charscount = 0;
uint8_t *result = NULL;
for (i = 0; in [ i ] !=0x0000; i++)
{
uint16_t unicode = in [ i ];
if (unicode >= 0x0000 && unicode <= 0x007f)
{
*tmp = (uint8_t)unicode;
tmp++;
outsize += 1;
}
else if (unicode >= 0x0080 && unicode <= 0x07ff)
{
*tmp = 0xc0 | (unicode >> 6);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 2;
}
else if (unicode >= 0x0800 && unicode <= 0xffff)
{
*tmp = 0xe0 | (unicode >> 12);
tmp += 1;
*tmp = 0x80 | (unicode >> 6 & 0x00ff);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 3;
}
}
*tmp = '\\0';
// *out = result;
return 0;
}
写的太乱大家见谅哈 |
|