Conversion between GB and Unicode in Wince

Source: Internet
Author: User

The characters in wince are unicode encoded, and the XML used for the project also needs to be unicode characters, while the LCD in the project is a GB character, there is a conversion problem between the two after google, we found that this type of conversion can only be performed in the form of a look-up table. We made the characters corresponding to unicode and GB code into a two-dimensional code table, and then searched and obtained. The total number of GB characters is 21791, And the binary search is used.

1. unicode conversion to GB

// The function converts unicode to GB. The code table must be sorted by unicode!

 

unsigned short  UnicodeTOGBK(unsigned short unicode){int low=0,high=TotalGBKNum-1,mid;while(low <= high){mid = (low+high)/2;int x=GBK2UNICODETBL[mid][1];if(GBK2UNICODETBL[mid][1] == unicode)return GBK2UNICODETBL[mid][0];if(GBK2UNICODETBL[mid][1]>unicode)high=mid-1;elselow=mid+1;}return 0xFFFF;}

 

2. Convert GB to unicode

// The function is to convert GB to unicode. The code table must be sorted by GB!

unsigned int gb2unicode(unsigned short *unicode,unsigned char*gb,int len) {     int i,j;     i=0;     for(j=0;i<len;j++)     {         if ((unsigned char)gb[j]<=0x80)         {             unicode[j]=(unsigned short)gb[j];             i++;         }        else         {             unicode[j]=ESM_getValidGBCode(*(unsigned short *)(gb+i));             i+=2;         } #ifdef Little2Big_Endianreverise_w(&unicode[j]);    #endif    }     return j*2; } 
unsigned short  ESM_getValidGBCode(unsigned short gb){int low=0,high=TotalGBKNum-1,mid;while(low <= high){mid = (low+high)/2;if(GBK2UNICODETBL[mid][0] == gb)return GBK2UNICODETBL[mid][1];if(GBK2UNICODETBL[mid][0]>gb)high=mid-1;elselow=mid+1;}return 0xFFFF;}

 

Corresponding table download link: http://download.csdn.net/detail/mjx91282041/4768949

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.