C#中string與byte[]的轉(zhuǎn)換幫助類-.NET教程,C#語言
更新時間:2008年03月09日 20:19:08 作者:
在寫c#程序時,string和byte[]之間的轉(zhuǎn)換比較煩,在移植一些老程序時感覺很不好。我在c#中使用des和tripledes時移植一塊老代碼時也遇到了同樣的情況。為了下次不為同樣的事情煩惱,就寫了下面的幫助類。
主要實現(xiàn)了以下的函數(shù)
代碼中出現(xiàn)的sidle是我的網(wǎng)名。
/**//*
* @author wuerping
* @version 1.0
* @date 2004/11/30
* @description:
*/
using system;
using system.text;
namespace sidlehelper
{
/**//// <summary>
/// summary description for strhelper.
/// 命名縮寫:
/// str: unicode string
/// arr: unicode array
/// hex: 二進制數(shù)據(jù)
/// hexbin: 二進制數(shù)據(jù)用ascii字符表示 例 字符1的hex是0x31表示為hexbin是 31
/// asc: ascii
/// uni: unicode
/// </summary>
public sealed class strhelper
{
hex與hexbin的轉(zhuǎn)換#region hex與hexbin的轉(zhuǎn)換
public static void hexbin2hex(byte[] bhexbin, byte[] bhex, int nlen)
{
for(int i=0; i<nlen/2; i++)
{
if(bhexbin[2*i] <0x41)
{
bhex[i] = convert.tobyte(((bhexbin[2*i] - 0x30)<<4) & 0xf0);
}
else
{
bhex[i] = convert.tobyte(((bhexbin[2*i] - 0x37)<<4) & 0xf0);
}
if(bhexbin[2*i+1] <0x41)
{
bhex[i] |= convert.tobyte((bhexbin[2*i+1] - 0x30) & 0x0f);
}
else
{
bhex[i] |= convert.tobyte((bhexbin[2*i+1] - 0x37) & 0x0f);
}
}
}
public static byte[] hexbin2hex(byte[] bhexbin, int nlen)
{
if(nlen%2 !=0)
return null;
byte[] bhex = new byte[nlen/2];
hexbin2hex(bhexbin, bhex, nlen);
return bhex;
}
public static void hex2hexbin(byte[] bhex, byte[] bhexbin, int nlen)
{
byte c;
for(int i=0;i<nlen;i++)
{
c = convert.tobyte((bhex[i]>>4) & 0x0f);
if(c < 0x0a)
{
bhexbin[2*i] = convert.tobyte(c + 0x30);
}
else
{
bhexbin[2*i] = convert.tobyte(c + 0x37);
}
c = convert.tobyte(bhex[i]&0x0f);
if(c < 0x0a)
{
bhexbin[2*i+1] = convert.tobyte(c + 0x30);
}
else
{
bhexbin[2*i+1] = convert.tobyte(c + 0x37);
}
}
}
public static byte[] hex2hexbin(byte[] bhex, int nlen)
{
byte[] bhexbin = new byte[nlen*2];
hex2hexbin(bhex, bhexbin, nlen);
return bhexbin;
}
#endregion
數(shù)組和字符串之間的轉(zhuǎn)化#region 數(shù)組和字符串之間的轉(zhuǎn)化
public static byte[] str2arr(string s)
{
return (new unicodeencoding()).getbytes(s);
}
public static string arr2str(byte[] buffer)
{
return (new unicodeencoding()).getstring(buffer, 0, buffer.length);
}
public static byte[] str2ascarr(string s)
{
return system.text.unicodeencoding.convert(system.text.encoding.unicode,
system.text.encoding.ascii,
str2arr(s));
}
public static byte[] str2hexascarr(string s)
{
byte[] hex = str2ascarr(s);
byte[] hexbin = hex2hexbin(hex, hex.length);
return hexbin;
}
public static string ascarr2str(byte[] b)
{
return system.text.unicodeencoding.unicode.getstring(
system.text.asciiencoding.convert(system.text.encoding.ascii,
system.text.encoding.unicode,
b)
);
}
public static string hexascarr2str(byte[] buffer)
{
byte[] b = hex2hexbin(buffer, buffer.length);
return ascarr2str(b);
}
#endregion
}
}
您可能感興趣的文章:
- C# Stream 和 byte[] 之間的轉(zhuǎn)換
- C# 字符串string和內(nèi)存流MemoryStream及比特數(shù)組byte[]之間相互轉(zhuǎn)換
- C# byte數(shù)組與Image相互轉(zhuǎn)換的方法
- C#中Byte[]和String之間轉(zhuǎn)換的方法
- C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實例教程
- C#中兩個byte如何相加
- C#中圖片.BYTE[]和base64string的轉(zhuǎn)換方法
- C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
- C#如何從byte[]中直接讀取Structure實例詳解
相關(guān)文章
在 ASP.NET Core 中自動啟用 CAP 事務(wù)詳情
本篇文章旨在描述如何在 ASP.NET Core項目中并以一種簡便的方式啟用CAP事務(wù),因為在我們的示例中都是直接演示比較直觀的方式,沒有進行封裝,有些初學(xué)者同學(xué)不太會,找到問我如何封裝,本篇文章主要基于 Entity Framework 來進行演示2021-10-10ASP.NET?Core使用功能開關(guān)控制路由訪問操作(續(xù))
這篇文章主要介紹了ASP.NET?Core使用功能開關(guān)控制路由訪問操作的(續(xù)),上一篇文章我們已經(jīng)介紹過一部份該相關(guān)內(nèi)容,??在本文,我們可以判斷當(dāng)前路由地址是否為調(diào)試地址,讓評估返回真,需要的小伙伴可以參考一下2022-02-02.NET?中配置從xml轉(zhuǎn)向json方法示例詳解
這篇文章主要為大家介紹了.NET?中配置從xml轉(zhuǎn)向json方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11.Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsSnapshot
這篇文章主要介紹了.Net?Core配置文件讀取IOptions,IOptionsMonitor,IOptionsSnapshot,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09深入分析XmlSerializer對象的Xml序列化與反序列化的示例詳解
本篇文章是對XmlSerializer 對象的Xml序列化與反序列化的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05ASP.NET MVC Web API HttpClient簡介
依稀還記得那個時候用WebClient,HttpWebRequest來發(fā)送一個請求,現(xiàn)在ASP.NET MVC4中自帶了一個類HttpClient;需要的朋友可以參考下2012-11-11