本帖最后由 爱我别走 于 2013-7-22 01:34 编辑
方法一,如果你的单片机存储空间够大,你可以用查表发。附件里面有一个gb2uni.h的文件, 可以很方便的在GBK和Unicode码之间相互转换。
方法二,使用字库芯片,如高通GT21L16S2W,***给你,实测可用。通过你的描述,
我推测你应该是在做单片机控制GPRS模块收发短信吧。
/*-------------------------------------------
引脚连接宏定义
------------------------------------------------*/
***it GT_CS = P2^0;
***it GT_SO = P2^1;
***it GT_SI = P2^2;
***it GT_CLK = P2^3;
/*--------------------------------------------------
函数名 :ReadByte
功能 :读取任意地址数据
输入参数:
输出参数:
--------------------------------------------------- */
unsigned char ReadByte(unsigned long Address)
{
unsigned char i = 0,j = 0,temp = 0x0B;
GT_CS = 0;
//发送指令
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI = temp&0x80;
temp <<= 1;
GT_CLK = 1;
}
//发送地址
for(j=1; j<4; j++)
{
temp = ( (unsigned char*)&Address )[j] ;
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI =temp & 0x80;
temp <<= 1;
GT_CLK = 1;
}
}
//发送空字节
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI = 0;
GT_CLK = 1;
}
//接收数据
for(i=0; i<8; i++)
{
GT_CLK = 0;
temp <<= 1;
if(GT_SO)
temp |= 0x01;
else
temp &= 0xFE;
GT_CLK = 1;
}
GT_CS = 1;
return temp;
}
/*--------------------------------------------------
函数名 :UnicodeToGB2312
功能 :Unicode 到GB2312 内码转换程序
输入参数:srcCode Unicode码
输出参数:destCode GB2312码
--------------------------------------------------- */
void UnicodeToGB2312(unsigned char*srcCode,unsigned char*destCode)
{
unsigned long ptr;
unsigned char result=0;
unsigned int h;
unsigned int Code;
Code = *srcCode++;
Code = (Code<<8) + *srcCode;
if(Code<0xa0) result=1;
else if(Code<=0xf7) h=Code-160;
else if(Code<0x2c7) result=1;
else if(Code<=0x2c9) h=Code-160-463;
else if(Code<0x2010) result=1;
else if(Code<=0x2312) h=Code-160-463-7494;
else if(Code<0x2460) result=1;
else if(Code<=0x2642) h=Code-160-463-7494-333;
else if(Code<0x3000) result=1;
else if(Code<=0x3017) h=Code-160-463-7494-333-2493;
else if(Code<0x3220) result=1;
else if(Code<=0x3229) h=Code-160-463-7494-333-2493-520;
else if(Code<0x4e00) result=1;
else if(Code<=0x9b54) h=Code-160-463-7494-333-2493-520-7126;
else if(Code<0x9c7c) result=1;
else if(Code<=0x9ce2) h=Code-160-463-7494-333-2493-520-7126-295;
else if(Code<0x9e1f) result=1;
else if(Code<=0x9fa0) h=Code-160-463-7494-333-2493-520-7126-295-316;
else if(Code<0xe76c) result=1;
else if(Code<=0xe774) h=Code-160-463-7494-333-2493-520-7126-295-316-18379;
else if(Code<0xff00) result=1;
else if(Code<=0xff5f) h=Code-160-463-7494-333-2493-520-7126-295-316-18379-6027;
else if(Code<0xffe0) result=1;
else if(Code<=0xffe5) h=Code-160-463-7494-333-2493-520-7126-295-316-18379-6027-128;
else result=1;
if(result==0)
{
ptr = 0x67d70 + (h<<1);
*destCode++ = ReadByte(ptr++);
*destCode = ReadByte(ptr);
}
else
{
*destCode++ =*srcCode;
*destCode = ' ';
}
}
本帖最后由 爱我别走 于 2013-7-22 01:34 编辑
方法一,如果你的单片机存储空间够大,你可以用查表发。附件里面有一个gb2uni.h的文件, 可以很方便的在GBK和Unicode码之间相互转换。
方法二,使用字库芯片,如高通GT21L16S2W,***给你,实测可用。通过你的描述,
我推测你应该是在做单片机控制GPRS模块收发短信吧。
/*-------------------------------------------
引脚连接宏定义
------------------------------------------------*/
***it GT_CS = P2^0;
***it GT_SO = P2^1;
***it GT_SI = P2^2;
***it GT_CLK = P2^3;
/*--------------------------------------------------
函数名 :ReadByte
功能 :读取任意地址数据
输入参数:
输出参数:
--------------------------------------------------- */
unsigned char ReadByte(unsigned long Address)
{
unsigned char i = 0,j = 0,temp = 0x0B;
GT_CS = 0;
//发送指令
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI = temp&0x80;
temp <<= 1;
GT_CLK = 1;
}
//发送地址
for(j=1; j<4; j++)
{
temp = ( (unsigned char*)&Address )[j] ;
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI =temp & 0x80;
temp <<= 1;
GT_CLK = 1;
}
}
//发送空字节
for(i=0; i<8; i++)
{
GT_CLK = 0;
GT_SI = 0;
GT_CLK = 1;
}
//接收数据
for(i=0; i<8; i++)
{
GT_CLK = 0;
temp <<= 1;
if(GT_SO)
temp |= 0x01;
else
temp &= 0xFE;
GT_CLK = 1;
}
GT_CS = 1;
return temp;
}
/*--------------------------------------------------
函数名 :UnicodeToGB2312
功能 :Unicode 到GB2312 内码转换程序
输入参数:srcCode Unicode码
输出参数:destCode GB2312码
--------------------------------------------------- */
void UnicodeToGB2312(unsigned char*srcCode,unsigned char*destCode)
{
unsigned long ptr;
unsigned char result=0;
unsigned int h;
unsigned int Code;
Code = *srcCode++;
Code = (Code<<8) + *srcCode;
if(Code<0xa0) result=1;
else if(Code<=0xf7) h=Code-160;
else if(Code<0x2c7) result=1;
else if(Code<=0x2c9) h=Code-160-463;
else if(Code<0x2010) result=1;
else if(Code<=0x2312) h=Code-160-463-7494;
else if(Code<0x2460) result=1;
else if(Code<=0x2642) h=Code-160-463-7494-333;
else if(Code<0x3000) result=1;
else if(Code<=0x3017) h=Code-160-463-7494-333-2493;
else if(Code<0x3220) result=1;
else if(Code<=0x3229) h=Code-160-463-7494-333-2493-520;
else if(Code<0x4e00) result=1;
else if(Code<=0x9b54) h=Code-160-463-7494-333-2493-520-7126;
else if(Code<0x9c7c) result=1;
else if(Code<=0x9ce2) h=Code-160-463-7494-333-2493-520-7126-295;
else if(Code<0x9e1f) result=1;
else if(Code<=0x9fa0) h=Code-160-463-7494-333-2493-520-7126-295-316;
else if(Code<0xe76c) result=1;
else if(Code<=0xe774) h=Code-160-463-7494-333-2493-520-7126-295-316-18379;
else if(Code<0xff00) result=1;
else if(Code<=0xff5f) h=Code-160-463-7494-333-2493-520-7126-295-316-18379-6027;
else if(Code<0xffe0) result=1;
else if(Code<=0xffe5) h=Code-160-463-7494-333-2493-520-7126-295-316-18379-6027-128;
else result=1;
if(result==0)
{
ptr = 0x67d70 + (h<<1);
*destCode++ = ReadByte(ptr++);
*destCode = ReadByte(ptr);
}
else
{
*destCode++ =*srcCode;
*destCode = ' ';
}
}
举报