C# 字符串重编码——任意字符串转化为全字母串
来源:互联网 发布:苹果5s怎么关闭网络 编辑:程序博客网 时间:2024/06/05 20:17
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ClearDir{ public class Encoder { public static void example() { String data = "test encode"; string encode = Encode(data); string decode = Decode(encode); bool b = data.Equals(decode); bool b2 = b; } /// <summary> /// 转码data为全字母串,并添加前缀 /// </summary> public static string Encode(string data) { string str = data; if (!data.StartsWith("ALPHABETCODE@")) { str = "ALPHABETCODE@" + EncodeAlphabet(data); } return str; } /// <summary> /// 解析字母串为原有串 /// </summary> public static string Decode(string data) { string str = data; if (data.StartsWith("ALPHABETCODE@")) { str = DecodeAlphabet(data.Substring("ALPHABETCODE@".Length)); } return str; } # region 字符串字母编码逻辑 /// <summary> /// 转化为字母字符串 /// </summary> public static string EncodeAlphabet(string data) { byte[] B = Encoding.UTF8.GetBytes(data); return ToStr(B); } /// <summary> /// 每个字节转化为两个字母 /// </summary> private static string ToStr(byte[] B) { StringBuilder Str = new StringBuilder(); foreach (byte b in B) { Str.Append(ToStr(b)); } return Str.ToString(); } private static string ToStr(byte b) { return "" + ToChar(b / 16) + ToChar(b % 16); } private static char ToChar(int n) { return (char)('a' + n); } /// <summary> /// 解析字母字符串 /// </summary> public static string DecodeAlphabet(string data) { byte[] B = new byte[data.Length / 2]; char[] C = data.ToCharArray(); for (int i = 0; i < C.Length; i += 2) { byte b = ToByte(C[i], C[i + 1]); B[i / 2] = b; } return Encoding.UTF8.GetString(B); } /// <summary> /// 每两个字母还原为一个字节 /// </summary> private static byte ToByte(char a1, char a2) { return (byte)((a1 - 'a') * 16 + (a2 - 'a')); } # endregion }}
阅读全文
0 0
- C# 字符串重编码——任意字符串转化为全字母串
- 华为机试——任意正整数编码为字符串
- 字符串转化为unicode编码
- 字符串中大写字母转化为小写字母
- Java、jsp判断字符串全为字母
- 数字字符串转化字母字符串
- C#中判断字符串中是否有汉字及将汉字转化为字母缩写
- 如何把字符串转化为unicode编码
- 将普通字符串转化为Unicode编码
- java把字符串转化为unicode编码
- QT 十六进制字符串转化为十六进制编码
- 第十章p202的程序:将一个全是字母的字符串转化为大写(用栈来传递)
- 整数转化为任意n进制的字符串
- 使用python将任意字符串转化为列表
- C# 判断字符串能否转化为日期
- c#将xml字符串转化为 DataTable
- C# 字符串转化为时间格式
- C# 字符串转化为时间格式
- Xamarin.Forms 用户界面——控件——布局——创建自定义布局
- Android source code
- 欢迎使用CSDN-markdown编辑器
- idtcpserver demo
- Hibernate一对多的关系
- C# 字符串重编码——任意字符串转化为全字母串
- unity 3D c# 协程的典型用法-Unity官方案例精讲笔记
- 判断二叉树是否为二叉搜索树
- 醒
- java编程思想笔记-并发之并发锁synchronized详解(二)
- Xamarin.Forms 用户界面——控件——ListView
- tp3.2.3无限级分类
- XML约束DTD
- Linux 新增磁盘,重新挂载分区