c#版 native2ascii与ascii2native工具

在.net反编译的时候或者是查看网页源码中都能看到类似"\u7f16\u7801\u8f6c\u6362"的编码 ,阅读起来很是不方便,于是从网上搜索编码解码工具,只搜到一个html版本的native2ascii,网页版本的感觉不是很方便,就自己做了一个winform的工具

设计界面如图

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8using Microsoft.VisualBasic.Devices;
  9namespace native2ascii
 10ExpandedBlockStart.gifContractedBlock.gif{
 11    public partial class Form1 : Form
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 13        public Form1()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 15            InitializeComponent();
 16        }

 17
 18        private void button1_Click(object sender, EventArgs e)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 20            this.textBox1.Text = native2ascii2(textBox1.Text);
 21        }

 22
 23        //native2ascii method
 24        public static String native2ascii(String str)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 26            int code;
 27            char[] chars = str.ToCharArray();
 28            StringBuilder sb = new StringBuilder(255);
 29            for (int i = 0; i < chars.Length; i++)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 31                char c = chars[i];
 32                if (c > 255)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 34                    sb.Append("\\u");
 35                    code = (c >> 8);
 36                    string tmp = code.ToString("X");
 37                    if (tmp.Length == 1) sb.Append("0");
 38                    sb.Append(tmp);
 39                    code = (c & 0xFF);
 40                    tmp = code.ToString("X");
 41                    if (tmp.Length == 1) sb.Append("0");
 42                    sb.Append(tmp);
 43                }

 44                else
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 46                    sb.Append(c);
 47                }

 48
 49            }

 50            return (sb.ToString());
 51        }

 52        public static String native2ascii2(String str)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54            string outStr = "";
 55            if (!string.IsNullOrEmpty(str))
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 57                for (int i = 0; i < str.Length; i++)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 59                    //将中文字符转为10进制整数,然后转为16进制unicode字符
 60                    outStr += "\\u" + ((int)str[i]).ToString("x");
 61                }

 62            }

 63            return outStr;
 64        
 65        }

 66        //end method
 67       //ascii2native method
 68        public static String ascii2native(String str)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 70            string outStr = "";
 71            if (!string.IsNullOrEmpty(str))
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 73                string[] strlist = str.Replace("\\""").Split('u');
 74                try
 75ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 76                    for (int i = 1; i < strlist.Length; i++)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 78                        //将unicode字符转为10进制整数,然后转为char中文字符
 79                        outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
 80                    }

 81                }

 82                catch (FormatException ex)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 84                    outStr = ex.Message;
 85                }

 86                
 87            }

 88            return outStr;
 89        
 90        }

 91        //end method
 92
 93        private void button2_Click(object sender, EventArgs e)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 95            textBox1.Text = ascii2native(textBox1.Text);
 96
 97        }
 
 98
 99        
100ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////   <summary> 
101        ///   转换为简体中文 
102        ///   </summary> 

103        public static string ToSChinese(string str)
104ExpandedSubBlockStart.gifContractedSubBlock.gif        {
105            //return Strings.StrConv(str, VbStrConv.SimplifiedChinese, 0);
106            return Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0);
107        }

108
109ExpandedSubBlockStart.gifContractedSubBlock.gif        /**////   <summary> 
110        ///   转换为繁体中文 
111        ///   </summary> 

112        public static string ToTChinese(string str)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        {
114            return Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0);
115        }

116
117        private void button3_Click(object sender, EventArgs e)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        {
119            textBox1.Text = ToTChinese(textBox1.Text);
120        }

121
122        private void button4_Click(object sender, EventArgs e)
123ExpandedSubBlockStart.gifContractedSubBlock.gif        {
124            textBox1.Text = ToSChinese(textBox1.Text);
125        }

126
127     
128    }

129}

 

注:代码中的native2ascii 与native2ascii2方法都是实现从中文转换unicode方法

简体转繁体与繁体转简体用到了VB中的函数因此需要引用Visual Basic

引用方法

右击 引用 选择 添加引用  在引用.net库选择 Microsoft Visual Basic

 

 

转载于:https://www.cnblogs.com/shidongjie/archive/2009/11/20/1607066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:那么既然都已经推出了 unicode 统一编码字符集,为什么不统一全部使用 ucs-2/utf-16 编码呢?这是因为其实对于英文使用国家来说,字符基本上都是 ASCII 字符,使用 utf-8 编码一个字节代表一个字符很常见,如果使用 ucs-2/utf-16 编码反而会浪费空间。 引用:这段代码的核心就是我们上面提到的 utf-8 和 utf-16 转换的公式。我们详细解析一下这个函数,先假设传递过来的字符串是“a中文”,对应 utf-8 编码十六进制是 “0x610xE40xB80xAD0xE60x960x87”,转换步骤如下: 引用:发现了么?dalvik 代码中并没有对 4 字节 utf-8 编码的字符串进行处理,而 ART 中专门用了很详细的注释说明了针对 4 字节编码的 utf-8 需要转成代理对(surrogate pair)!为什么之前 Android 本没有针对 4 字节编码进行处理?我的一个推测是:可能老本的 Android 系统使用的是 ucs-2 编码,并没有对 BMP 之外的平面集做处理,所以也不存在 4 字节的 utf-8,在扩展为 utf-16 编码之后,自然而然就需要额外对 4 字节的 utf-8 进行转换成代理对的操作。 Hash'native2ascii' 不是内部或外部命令,也不是可运行的程序或批处理文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Android Native 开发之 NewString 与 NewStringUtf 解析](https://blog.csdn.net/u201011221/article/details/112590839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值