Linux下從unicode到UTF-8的轉換,含執行個體

來源:互聯網
上載者:User

有關unicode及UTF-8和其它字元編碼的介紹請參考阮一峰的日誌:


字元編碼筆記:ASCII,Unicode和UTF-8原以為直接使用widechartomultibyte就可以直接搞定,到後來才發現這個程式是在windows平台下用的,Linux下根本找不到。後來找到了一個Linux下台下的程式,wcstombs() ,需要瞭解的朋友可以自己到網上Google一下。但是由於自己應用方面的限制也用不了。實在沒辦法就只好自己寫了,代碼如下:
                                                                                                              int  UnicodeToUTF_8(unsigned long * InPutStr, int InPutStrLen,              char * OutPutStr){    int  i=0,offset=0;    InPutStrLen = InPutStrLen/sizeof(unsigned long);    for(i=0; i<InPutStrLen; i++)    {           if(InPutStr[i] <= 0x0000007f)        {               OutPutStr[offset++]= (char)(InPutStr[i] & 0x0000007f);        }           else if(InPutStr[i] >= 0x00000080 && InPutStr[i] <= 0x000007ff  )        {               OutPutStr[offset++] = (char)(((InPutStr[i] & 0x000007c0) >>6 ) | 0x000000e0 );            OutPutStr[offset++] = (char)( (InPutStr[i] & 0x0000003f) | 0x00000080 );        }           else if(InPutStr[i] >= 0x00000800 && InPutStr[i] <= 0x0000ffff )        {               OutPutStr[offset++] = (char)(((InPutStr[i] & 0x0000f000) >> 12 ) | 0x000000e0 );            OutPutStr[offset++] = (char)(((InPutStr[i] & 0x00000fc0)>>6) | 0x00000080 );            OutPutStr[offset++] = (char)((InPutStr[i] & 0x0000003f) | 0x00000080);        }           else if(InPutStr[i] >= 0x00010000 && InPutStr[i] <= 0x0010ffff )        {               OutPutStr[offset++] = (char)((((InPutStr[i] & 0x001c0000) >> 16 ) | 0x000000f0 ));             OutPutStr[offset++] = (char)(((InPutStr[i] & 0x0003f000)>>12) | 0x00000080 );            OutPutStr[offset++] = (char)(((InPutStr[i] & 0x00000fc0)>>6) | 0x00000080 );            OutPutStr[offset++] = (char)((InPutStr[i] & 0x0000003f) | 0x00000080);            }       }       return offset;      }

經過驗證,搞定了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.