C#中string與byte[]的轉(zhuǎn)換幫助類-.NET教程,C#語(yǔ)言
更新時(shí)間:2008年03月09日 20:19:08 作者:
在寫(xiě)c#程序時(shí),string和byte[]之間的轉(zhuǎn)換比較煩,在移植一些老程序時(shí)感覺(jué)很不好。我在c#中使用des和tripledes時(shí)移植一塊老代碼時(shí)也遇到了同樣的情況。為了下次不為同樣的事情煩惱,就寫(xiě)了下面的幫助類。
主要實(shí)現(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.
/// 命名縮寫(xiě):
/// str: unicode string
/// arr: unicode array
/// hex: 二進(jìn)制數(shù)據(jù)
/// hexbin: 二進(jìn)制數(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及比特?cái)?shù)組byte[]之間相互轉(zhuǎn)換
- C# byte數(shù)組與Image相互轉(zhuǎn)換的方法
- C#中Byte[]和String之間轉(zhuǎn)換的方法
- C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實(shí)例教程
- C#中兩個(gè)byte如何相加
- C#中圖片.BYTE[]和base64string的轉(zhuǎn)換方法
- C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
- C#如何從byte[]中直接讀取Structure實(shí)例詳解
相關(guān)文章
ASP.NET webUploader上傳大視頻文件相關(guān)web.config配置
本文主要介紹了webUploader上傳大視頻文件相關(guān)web.config的配置。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
ASP.NET學(xué)習(xí)中常見(jiàn)錯(cuò)誤總結(jié)歸納
這篇文章主要介紹了asp.net學(xué)習(xí)過(guò)程中碰到的常見(jiàn)錯(cuò)誤的解決方法,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09
.NET 6新特性試用Timer類之PeriodicTimer?
這篇文章主要介紹了.NET 6新特性試用Timer類之PeriodicTimer,PeriodicTimer與其他Timer需要?jiǎng)?chuàng)建事件回調(diào)不同,下,下面文章詳細(xì)介紹PeriodicTimer的使用方式,需要的朋友可以參考一下2022-02-02
.net程序開(kāi)發(fā)IOC控制反轉(zhuǎn)和DI依賴注入詳解
這篇文章主要為大家介紹了.net程序開(kāi)發(fā)IOC控制反轉(zhuǎn)和DI依賴注入示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
利用.NET 開(kāi)發(fā)服務(wù)器 應(yīng)用管理工具
這篇文章主要介紹如何利用.NET 開(kāi)發(fā)一個(gè)應(yīng)用管理工具的服務(wù)器,文章回先聊背景接著其是喲美好方法,需要的的小伙伴可以參考一下小面文章的具體內(nèi)容2021-10-10
.Net性能調(diào)優(yōu)-ArrayPool詳情
ArrayPool具有高性能 托管 數(shù)組緩沖池,可重復(fù)使用,用 租用 空間的方式代替 重新分配 數(shù)組空間的行為的特點(diǎn)及可以在頻繁創(chuàng)建和銷毀數(shù)組的情況下 提高性能 ,減少垃圾回收器的壓力的優(yōu)點(diǎn),下面文章內(nèi)容將詳細(xì)對(duì)其做介紹,需要的朋友可以參考一下2021-09-09

