c# UTF-16轉UTF-8 互轉

来源:https://www.cnblogs.com/yuanzijian-ruiec/archive/2018/10/01/9734269.html
-Advertisement-
Play Games

/// /// UTF-16轉UTF-8 /// /// /// public static string UTF16To8(string str) { string res; int i, len, c; res =... ...


  /// <summary>
        /// UTF-16轉UTF-8
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UTF16To8(string str)
        {
            string res;
            int i, len, c;
            res = "";
            len = str.Length;
            for (i = 0; i < len; i++)
            {
                c = Convert.ToByte(str[i]);
                if ((c >= 0x0001) && (c <= 0x007F))
                {
                    res += str.CharAt(i);
                }
                else if (c > 0x07FF)
                {
                    res += Convert.ToChar(0xE0 | ((c >> 12) & 0x0F));
                    res += Convert.ToChar(0x80 | ((c >> 6) & 0x3F));
                    res += Convert.ToChar(0x80 | ((c >> 0) & 0x3F));
                }
                else
                {
                    res += Convert.ToChar(0xC0 | ((c >> 6) & 0x1F));
                    res += Convert.ToChar(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return res;
        }

 

 /// <summary>
        /// UTF-8轉UTF-16
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UTF8To16(string str)
        {
            string res;
            int i, len, c;
            int char2, char3;
            res = "";
            len = str.Length;
            i = 0;
            while (i < len)
            {
                c = Convert.ToByte(str[i++]);
                switch (c >> 4)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        // 0xxxxxxx
                        res += str.CharAt(i - 1);
                        break;
                    case 12:
                    case 13:
                        // 110x xxxx 10xx xxxx
                        char2 = Convert.ToByte(str[i++]);
                        res += Convert.ToChar(((c & 0x1F) << 6) | (char2 & 0x3F));
                        break;
                    case 14:
                        // 1110 xxxx 10xx xxxx 10xx xxxx
                        char2 = Convert.ToByte(str[i++]);
                        char3 = Convert.ToByte(str[i++]);
                        res += Convert.ToChar(((c & 0x0F) << 12) |
                            ((char2 & 0x3F) << 6) |
                            ((char3 & 0x3F) << 0));
                        break;
                }
            }
            return res;
        }
public static class te
    {
        /// <summary>
        /// 返回指定位置字元
        /// </summary>
        /// <param name="str">原字元串</param>
        /// <param name="index">字元索引,長度超出時返回:' '</param>
        /// <returns></returns>
        public static char CharAt(this string str, int index)
        {
            if (index > str.Length)
                return ' ';

            string res = str.Substring(index, 1);
            return Convert.ToChar(res);
        }
    }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 導航: 1、創建進程的兩種方式2、Process的方法3、進程間的通訊1,進程隊列Queue--先進先出4、進程間的通訊2,管道通訊 Pipe5、進程間的數據共用,Manager6、多進程同步問題7、進程池Pool python中多進程可以解決cpython解釋器多線程中GIL存在的問題,可以利用C ...
  • Dapper作為.NET生態中廣為人知的輕量級ORM類庫在.NET Core里仍能被有效利用,並且其不但可以連通SQL Server資料庫還提供對其它資料庫,比如MySQL的支持。這裡試驗了一下通過Dapper連接MySQL的方法。 MySQL 可以選擇直接安裝在原生系統中或是Docker里。 "O ...
  • 一、 新建一個Winform項目 二、使用Nuget添加引用 安裝下圖中紅色框住的兩個程式包 安裝完後發現安裝瞭如下圖的程式包,這是因為上述兩個程式包存在對其它程式包的依賴。 三、編寫程式 1. 窗體設計,攝像頭是下拉列表(cmbCamera,控制項命名,下同),雖然示例只用到一個攝像頭,但是該Dem ...
  • /// /// 生成驗證碼圖片 /// /// public byte[] GetVerifyCode() { int codeW = 80; int codeH = 40; int fontSize = 18; ... ...
  • /// /// 對Url進行編碼 /// /// url /// 編碼字元是否轉成大寫,範例,"http://"轉成"http%3A%2F%2F" public static string UrlEncode(string url, bool isUpper = false) { ... ...
  • using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using Sy... ...
  • using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.IO; using System.Web; using System.Net.Mai... ...
  • using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.L... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...