| 網(wǎng)站首頁 | 關(guān)于我們 | 開發(fā)優(yōu)勢 | 產(chǎn)品展示 |
| 合作企業(yè) | 新聞動態(tài) | 聯(lián)系我們 | 電話聯(lián)系 |
文章作者:濟(jì)南軟件開發(fā) 時間:2016年11月08日
為了適應(yīng)某種特殊需要,字符需要根據(jù)規(guī)則進(jìn)行轉(zhuǎn)碼,便于傳輸、展現(xiàn)以及其他操作等。
看看下面的轉(zhuǎn)碼,就知道他的用處了。
1、字符串轉(zhuǎn)碼
根據(jù)原編碼格式與目標(biāo)編碼格式,完成轉(zhuǎn)換。不過可能出現(xiàn)亂碼哦。上一章已經(jīng)介紹過了。
代碼:
復(fù)制代碼
/// <summary>
/// 字符串編碼轉(zhuǎn)換
/// </summary>
/// <param name="srcEncoding">原編碼</param>
/// <param name="dstEncoding">目標(biāo)編碼</param>
/// <param name="srcBytes">原字符串</param>
/// <returns>字符串</returns>
public static string TransferEncoding(Encoding srcEncoding, Encoding dstEncoding, string srcStr)
{
byte[] srcBytes = srcEncoding.GetBytes(srcStr);
byte[] bytes = Encoding.Convert(srcEncoding, dstEncoding, srcBytes);
return dstEncoding.GetString(bytes);
}
復(fù)制代碼
測試用例:
input = "歡迎來到轉(zhuǎn)碼世界!";
result = Transfer.TransferEncoding(Encoding.Default, Encoding.UTF8, input);//歡迎來到轉(zhuǎn)碼世界!
Console.WriteLine("TransferEncoding 結(jié)果:{0}",result);
result = Transfer.TransferEncoding(Encoding.UTF8,Encoding.Default,result);
Console.WriteLine("TransferEncoding 反轉(zhuǎn)碼結(jié)果:{0}", result);//歡迎來到轉(zhuǎn)碼世界!
2、Html轉(zhuǎn)碼
要點:將字符 < 和 > 在嵌入到文本塊中時被編碼為 < 和 >
如果在 HTTP 流中傳遞空白和標(biāo)點之類的字符,則它們在接收端可能會被錯誤地解釋。 HTML 編碼將 HTML 中不允許使用的字符轉(zhuǎn)換為等效字符實體;HTML 解碼會反轉(zhuǎn)此編碼過程。 例如,為進(jìn)行 HTTP 傳輸,字符 < 和 > 在嵌入到文本塊中時被編碼為 < 和 >。
要編碼或解碼 Web 應(yīng)用程序之外的值,請使用 WebUtility 類。
轉(zhuǎn)碼:
復(fù)制代碼
/// <summary>
/// html轉(zhuǎn)碼
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string HtmlEncode(string html)
{
return HttpUtility.HtmlEncode(html);//System.Net.WebUtility.HtmlEncode(html);
}
復(fù)制代碼
解碼:
復(fù)制代碼
/// <summary>
/// html解碼
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string HtmlDecode(string html)
{
return HttpUtility.HtmlDecode(html);//System.Net.WebUtility.HtmlDecode(html);
}
復(fù)制代碼
測試用例:
復(fù)制代碼
//HtmlEncode測試
result = string.Empty;
input = "<head>you & me</head>";
result = Transfer.HtmlEncode(input); //結(jié)果: <head>you & me</head>
Console.WriteLine("Html Encode: {0}", result);
result = Transfer.HtmlDecode(result);//結(jié)果: <head>you & me</head>
Console.WriteLine("Html Decode: {0}",result);
復(fù)制代碼
3、Url編碼
要點:對URL的特殊字符進(jìn)行轉(zhuǎn)義,使其合法。
可用于對整個 URL(包括查詢字符串值)進(jìn)行編碼的方法。如果在 HTTP 流中傳遞空白和標(biāo)點之類的字符,則它們在接收端可能會被錯誤地解釋。 URL 編碼將 URL 中不允許使用的字符轉(zhuǎn)換為等效字符實體;URL 解碼會反轉(zhuǎn)此編碼過程。 例如,當(dāng)嵌入到要在 URL 中傳輸?shù)奈谋緣K中時,字符 < 和 > 分別被編碼為 %3c 和 %3e。
默認(rèn)情況下 HttpUtility.UrlEncode 方法使用 UTF-8 編碼。 因此,使用 UrlEncode 方法提供結(jié)果相同使用 UrlEncode 方法并指定 UTF8 作為第二個參數(shù)。
UrlEncode 是一種簡便方式,用于在運行時從 ASP.NET 應(yīng)用程序訪問 UrlEncode 方法。 在內(nèi)部,UrlEncode 使用 UrlEncode 方法輸入字符串。
要編碼或解碼 Web 應(yīng)用程序之外的值,請使用 WebUtility 類。
HttpUtility的命名空間:System.Web
Url轉(zhuǎn)碼
復(fù)制代碼
/// <summary>
/// Url轉(zhuǎn)碼
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string UrlEncode(string url)
{
return HttpUtility.UrlEncode(url);
}
復(fù)制代碼
Url解碼
復(fù)制代碼
/// <summary>
/// Url解碼
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string UrlDecode(string url)
{
return HttpUtility.UrlDecode(url);
}
復(fù)制代碼
測試用例:
//UrlEncode測試
input = "http://www.baidu.com?username=<find>&content=ab c";
result = Transfer.UrlEncode(input);//結(jié)果:http%3a%2f%2fwww.baidu.com%3fusername%3d%3cfind%3e%26content%3dab+c
Console.WriteLine("Url Encode: {0}",result);
result = Transfer.UrlDecode(result);//結(jié)果:http://www.baidu.com?username=<find>&content=ab c
Console.WriteLine("Url Decode: {0}", result);
4、Base64
Base64內(nèi)容傳送編碼被設(shè)計用來把任意序列的8位字節(jié)描述為一種不易被人直接識別的形式。
要點:采用64個基本的ASCII碼字符對數(shù)據(jù)進(jìn)行重新編碼,用于加密和傳輸。
編碼規(guī)則
Base64編碼的思想是是采用64個基本的ASCII碼字符對數(shù)據(jù)進(jìn)行重新編碼。它將需要編碼的數(shù)據(jù)拆分成字節(jié)數(shù)組。以3個字節(jié)為一組。按順序排列24 位數(shù)據(jù),再把這24位數(shù)據(jù)分成4組,即每組6位。再在每組的的最高位前補兩個0湊足一個字節(jié)。這樣就把一個3字節(jié)為一組的數(shù)據(jù)重新編碼成了4個字節(jié)。當(dāng)所要編碼的數(shù)據(jù)的字節(jié)數(shù)不是3的整倍數(shù),也就是說在分組時最后一組不夠3個字節(jié)。這時在最后一組填充1到2個0字節(jié)。并在最后編碼完成后在結(jié)尾添加1到2個 “=”。
例:將對ABC進(jìn)行BASE64編碼:
1、首先取ABC對應(yīng)的ASCII碼值。A(65)B(66)C(67);
2、再取二進(jìn)制值A(chǔ)(01000001)B(01000010)C(01000011);
3、然后把這三個字節(jié)的二進(jìn)制碼接起來(010000010100001001000011);
4、 再以6位為單位分成4個數(shù)據(jù)塊,并在最高位填充兩個0后形成4個字節(jié)的編碼后的值,(00010000)(00010100)(00001001)(00000011),其中藍(lán)色部分為真實數(shù)據(jù);
5、再把這四個字節(jié)數(shù)據(jù)轉(zhuǎn)化成10進(jìn)制數(shù)得(16)(20)(9)(3);
6、最后根據(jù)BASE64給出的64個基本字符表,查出對應(yīng)的ASCII碼字符(Q)(U)(J)(D),這里的值實際就是數(shù)據(jù)在字符表中的索引。
BASE64字符表:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
應(yīng)用:
1、Base64編碼可用于在HTTP環(huán)境下傳遞較長的標(biāo)識信息
2、Base64 也會經(jīng)常用作一個簡單的“加密”來保護(hù)某些數(shù)據(jù),比如URL,而真正的加密通常都比較繁瑣。
代碼實現(xiàn)
Base64轉(zhuǎn)碼
復(fù)制代碼
/// <summary>
/// Base64轉(zhuǎn)碼
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToBase64(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(bytes);
}
復(fù)制代碼
Base64解碼:
復(fù)制代碼
/// <summary>
/// Base64字符串解碼
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string FromBase64(string input)
{
byte[] bytes = Convert.FromBase64String(input);
return Convert.ToBase64String(bytes);
}
復(fù)制代碼
測試用例:
//Base64轉(zhuǎn)碼測試
input = "Coming from the new world!";
result = Transfer.ToBase64(input); //結(jié)果:Q29taW5nIGZyb20gdGhlIG5ldyB3b3JsZCE=
Console.WriteLine("ToBase64 : {0}",result);
result = Transfer.FromBase64(result); //結(jié)果:Coming from the new world!
Console.WriteLine("FromBase64 : {0}", result);
5、BitConverter
將基礎(chǔ)數(shù)據(jù)類型與字節(jié)數(shù)組相互轉(zhuǎn)換。
BitConverter 類以一系列字節(jié)的形式有助于操控基本窗體中的值類型。 一個字節(jié)定義為一個 8 位無符號整數(shù)。 如下表所示,BitConverter 類包括用靜態(tài)方法將每個基元類型轉(zhuǎn)換成字節(jié)數(shù)組和將字節(jié)數(shù)組轉(zhuǎn)換成其基元類型。
Type
轉(zhuǎn)換為字節(jié)
從字節(jié)轉(zhuǎn)換
Boolean
GetBytes(Boolean)
ToBoolean
Char
GetBytes(Char)
ToChar
Double
GetBytes(Double)
- 或 -
DoubleToInt64Bits(Double)
ToDouble
- 或 -
Int64BitsToDouble
Int16
GetBytes(Int16)
ToInt16
Int32
GetBytes(Int32)
ToInt32
Int64
GetBytes(Int64)
ToInt64
Single
GetBytes(Single)
ToSingle
UInt16
GetBytes(UInt16)
ToUInt16
UInt32
GetBytes(UInt32)
ToUInt32
UInt64
GetBytes(UInt64)
ToUInt64
在這里我們以最基本的字符串類型為例說明。
byte轉(zhuǎn)為String
復(fù)制代碼
/// <summary>
/// 字節(jié)數(shù)組轉(zhuǎn)為字符串
/// 將指定的字節(jié)數(shù)組的每個元素的數(shù)值轉(zhuǎn)換為它的等效十六進(jìn)制字符串表示形式。
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string BitToString(byte[] bytes)
{
if (bytes == null)
{
return null;
}
//將指定的字節(jié)數(shù)組的每個元素的數(shù)值轉(zhuǎn)換為它的等效十六進(jìn)制字符串表示形式。
return BitConverter.ToString(bytes);
}
復(fù)制代碼
十六進(jìn)制string轉(zhuǎn)byte
復(fù)制代碼
/// <summary>
/// 將十六進(jìn)制字符串轉(zhuǎn)為字節(jié)數(shù)組
/// </summary>
/// <param name="bitStr"></param>
/// <returns></returns>
public static byte[] FromBitString(string bitStr)
{
if (bitStr == null)
{
return null;
}
string[] sInput = bitStr.Split("-".ToCharArray());
byte[] data = new byte[sInput.Length];
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
}
return data;
}
復(fù)制代碼
測試用例:
復(fù)制代碼
//bit轉(zhuǎn)碼
input = "Coming from the new world!";
result = Transfer.BitToString(Encoding.UTF8.GetBytes(input)); //結(jié)果: 43-6F-6D-69-6E-67-20-66-72-6F-6D-20-74-68-65-20-6E-65-77-20-77-6F-72-6C-64-21
Console.WriteLine("BitToString : {0}", result);
result = Encoding.UTF8.GetString(Transfer.FromBitString(result)); //結(jié)果:Coming from the new world!
C
想要了解更多詳情歡迎來電咨詢18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。