在SL年代,有第三方类库支持 http://encoding4silverlight.codeplex.com

在RT版本有点不兼容,一直没时间看。今天闲的卵疼,就来一段代码兼容一下RT版本,迟点整合到我自己类库,直接上码。

/*
* ****************************************************
* Copyright (c) Aimeast. All rights reserved.
* ****************************************************
*/ using App2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml; namespace DBCSCodePage
{
public sealed class DBCSEncoding : Encoding
{
private const char LEAD_BYTE_CHAR = '\uFFFE';
private char[] _dbcsToUnicode = null;
private ushort[] _unicodeToDbcs = null;
private string _webName = null; private static Dictionary<string, Tuple<char[], ushort[]>> _cache = null; static DBCSEncoding()
{
if (!BitConverter.IsLittleEndian)
throw new PlatformNotSupportedException("Not supported big endian platform."); _cache = new Dictionary<string, Tuple<char[], ushort[]>>();
} private DBCSEncoding() { } public async static Task<DBCSEncoding> GetDBCSEncoding(string name)
{
name = name.ToLower();
DBCSEncoding encoding = new DBCSEncoding();
encoding._webName = name;
if (_cache.ContainsKey(name))
{
var tuple = _cache[name];
encoding._dbcsToUnicode = tuple.Item1;
encoding._unicodeToDbcs = tuple.Item2;
return encoding;
} var dbcsToUnicode = new char[0x10000];
var unicodeToDbcs = new ushort[0x10000]; /*
* According to many feedbacks, add this automatic function for finding resource in revision 1.0.0.1.
* We suggest that use the old method as below if you understand how to embed the resource.
* Please make sure the *.bin file was correctly embedded if throw an exception at here.
*/
//using (Stream stream = typeof(DBCSEncoding).Assembly.GetManifestResourceStream(typeof(DBCSEncoding).Namespace + "." + name + ".bin"))
//App.Current.
var file = await GetPackagedFileAsync(); using (Stream stream = await file.OpenStreamForReadAsync())
using (BinaryReader reader = new BinaryReader(stream))
{
for (int i = 0; i < 0xffff; i++)
{
ushort u = reader.ReadUInt16();
unicodeToDbcs[i] = u;
}
for (int i = 0; i < 0xffff; i++)
{
ushort u = reader.ReadUInt16();
dbcsToUnicode[i] = (char)u;
}
} _cache[name] = new Tuple<char[], ushort[]>(dbcsToUnicode, unicodeToDbcs);
encoding._dbcsToUnicode = dbcsToUnicode;
encoding._unicodeToDbcs = unicodeToDbcs;
return encoding;
} public override int GetByteCount(char[] chars, int index, int count)
{
int byteCount = 0;
ushort u;
char c; for (int i = 0; i < count; index++, byteCount++, i++)
{
c = chars[index];
u = _unicodeToDbcs[c];
if (u > 0xff)
byteCount++;
} return byteCount;
} public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
int byteCount = 0;
ushort u;
char c; for (int i = 0; i < charCount; charIndex++, byteIndex++, byteCount++, i++)
{
c = chars[charIndex];
u = _unicodeToDbcs[c];
if (u == 0 && c != 0)
{
bytes[byteIndex] = 0x3f; // 0x3f == '?'
}
else if (u < 0x100)
{
bytes[byteIndex] = (byte)u;
}
else
{
bytes[byteIndex] = (byte)((u >> 8) & 0xff);
byteIndex++;
byteCount++;
bytes[byteIndex] = (byte)(u & 0xff);
}
} return byteCount;
} public override int GetCharCount(byte[] bytes, int index, int count)
{
return GetCharCount(bytes, index, count, null);
} private int GetCharCount(byte[] bytes, int index, int count, DBCSDecoder decoder)
{
int charCount = 0;
ushort u;
char c; for (int i = 0; i < count; index++, charCount++, i++)
{
u = 0;
if (decoder != null && decoder.pendingByte != 0)
{
u = decoder.pendingByte;
decoder.pendingByte = 0;
} u = (ushort)(u << 8 | bytes[index]);
c = _dbcsToUnicode[u];
if (c == LEAD_BYTE_CHAR)
{
if (i < count - 1)
{
index++;
i++;
}
else if (decoder != null)
{
decoder.pendingByte = bytes[index];
return charCount;
}
}
} return charCount;
} public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
return GetChars(bytes, byteIndex, byteCount, chars, charIndex, null);
} private int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, DBCSDecoder decoder)
{
int charCount = 0;
ushort u;
char c; for (int i = 0; i < byteCount; byteIndex++, charIndex++, charCount++, i++)
{
u = 0;
if (decoder != null && decoder.pendingByte != 0)
{
u = decoder.pendingByte;
decoder.pendingByte = 0;
} u = (ushort)(u << 8 | bytes[byteIndex]);
c = _dbcsToUnicode[u];
if (c == LEAD_BYTE_CHAR)
{
if (i < byteCount - 1)
{
byteIndex++;
i++;
u = (ushort)(u << 8 | bytes[byteIndex]);
c = _dbcsToUnicode[u];
}
else if (decoder == null)
{
c = '\0';
}
else
{
decoder.pendingByte = bytes[byteIndex];
return charCount;
}
}
if (c == 0 && u != 0)
chars[charIndex] = '?';
else
chars[charIndex] = c;
} return charCount;
} public override int GetMaxByteCount(int charCount)
{
if (charCount < 0)
throw new ArgumentOutOfRangeException("charCount");
long count = charCount + 1;
count *= 2;
if (count > int.MaxValue)
throw new ArgumentOutOfRangeException("charCount");
return (int)count; } public override int GetMaxCharCount(int byteCount)
{
if (byteCount < 0)
throw new ArgumentOutOfRangeException("byteCount");
long count = byteCount + 3;
if (count > int.MaxValue)
throw new ArgumentOutOfRangeException("byteCount");
return (int)count;
} public override Decoder GetDecoder()
{
return new DBCSDecoder(this);
} public override string WebName
{
get
{
return _webName;
}
} private sealed class DBCSDecoder : Decoder
{
private DBCSEncoding _encoding = null; public DBCSDecoder(DBCSEncoding encoding)
{
_encoding = encoding;
} public override int GetCharCount(byte[] bytes, int index, int count)
{
return _encoding.GetCharCount(bytes, index, count, this);
} public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
return _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex, this);
} public byte pendingByte;
} public static async Task<StorageFile> GetPackagedFileAsync(string fileName = @"Maps\gb2312.bin")
{
StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
return await installFolder.GetFileAsync(fileName);
}
}
}

  

这里需要用到一个包http://files.cnblogs.com/files/walleyekneel/Maps.zip  

记得要设置content

调用方式

var encoding = await DBCSEncoding.GetDBCSEncoding("gb2312");
var str = encoding.GetBytes("122222");

性能问题不经测试,作者不承担此风险

WinRT支持GB2312的更多相关文章

  1. [转] Sublime Text 3支持GB2312和GBK编码

    Sublime Text 3与Sublime Text 2的不同 其实有不少人写过如何让Sublime Text 2支持GB2312和GBK编码,例如这篇.基本原理就是先装好Package Contr ...

  2. 【linux学习笔记】Sublime Text3支持GB2312和GBK编码以及中文输入法

    几天在ubuntu15.10下使用Sublime Text3发现中文乱码,以及不能使用中文输入法(搜狗输入法linux版)的问题,捣鼓了半天,终于完善了,下面po一下我的解决方案. 一.支持GB231 ...

  3. 如何让.NET Core支持GB2312和GBK

    在.NET Core中,默认是不支持GB2312和GBK编码的. 例如我们如果新建一个.NET Core控制台项目,然后在其Main方法中使用如下代码: using System; using Sys ...

  4. Sublime Text 3技巧:支持GB2312和GBK编码

    From: http://blog.csdn.net/ubuntulover/article/details/21101979 Sublime Text 3与Sublime Text 2的不同 其实有 ...

  5. .net core默认不支持gb2312

    采集数据时,乱码,之前遇到过这个情况,于是老办法: 果断使用Encoding.GetEncoding(“GB2312”),抛异常.搜了下,是因为.net core默认不支持gb2312 所以,两个办法 ...

  6. C# 汉字转拼音(支持GB2312字符集中所有汉字)

    GB2312标准共收录6763个汉字,其中一级汉字3755个,二级汉字3008个. 分区表示  GB 2312中对所收汉字进行了“分区”处理,每区含有94个汉字/符号.这种表示方式也称为区位码. )- ...

  7. 3500常用汉字与标点符号(已排除不支持GB2312的)

    .?!,.::“”‘’…()<>〈〉[].,:;!?-'_"'()[]<>|&~;+-*/=<>0123456789ABCEFGHIJKLMNOP ...

  8. mono支持gb2312

    需要安装mono-locale-extras 输入命令 yum install -y mono-locale-extras 安装即可

  9. vim 支持gb2312

    vi  /etc/vimrc 中添加如下,并保存. set termencoding=encodingset fileencodings=utf-8,gbk,ucs-bom,cp936set shif ...

随机推荐

  1. RouterOS SOCKS代理服务器(官方文档翻译)

    SOCKS 是基于TCP应用层协议穿透防火墙的代理服务器,即使防火墙阻止了一些应用端口,也能通过SOCKS代理穿透.SOCKS协议是独立于应用层的,因此可以用于WWW.FTP.Telnet等等. 来至 ...

  2. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  3. Blueprint 编译概述

    转自:http://blog.csdn.net/cartzhang/article/details/39637269 一.术语 Blueprint,像C++语言一下的,在游戏中使用前需要编译.当你在B ...

  4. 【POJ】2096 Collecting Bugs(数学期望)

    题目 传送门:QWQ 分析 数学期望 用$ dp[i][j] $表示发现了在$ j $个子系统里面发现了$ i $个bug到$ s $个子系统里面发现了$ n $个bug需要的期望天数. $ dp[0 ...

  5. django rest_framework 框架的使用

    django 的中间件 csrf Require a present and correct csrfmiddlewaretoken for POST requests that have a CSR ...

  6. POI导出Excel和InputStream存储为文件

    POI导出Excel和InputStream存储为文件   本文需要说明的两个问题 InputStream如何保存到某个文件夹下 POI生成Excel POI操作utils类 代码如下.主要步骤如下: ...

  7. 关于python-生成HTMLTestRunner测试报告

    一.HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展,它可以生成 HTML的 测试报告. 1.首先要下 HTMLTestRunner.py 文件,将下载的文件 ...

  8. 2_bootstrap的环境搭建

    2.bootstrap环境搭建 2.1.下载资源 中文官网地址:http://d.bootcss.com/bootstrap-3.3.5.zip http://www.bootcss.com 2.2. ...

  9. Lrc歌词-开发标准

    LRC歌词是在其程序当中实现的专门用于MP3等歌词同步显示的标签式的纯文本文件,如今已经得到了广泛的运用.现就LRC歌词文件的格式规定详细说明,已供程序开发人员参考. LRC文件是纯文本文件,可以用记 ...

  10. Motion Blur

    [Motion Blur] 此篇介绍最简单的全局Motion Blur.算法是将当前帧与前一帧按某一比例混合.这是一个过程,例如有10帧,在第1帧中,只有第1帧原图,第2帧中有1.2帧原图,第3帧中会 ...