# 怎样将字符串转换成十六进制以及其逆转换（翻译四）

原文连接：

HexConverter10282006021521AM/HexConverter.aspx

背景

//为此我用了一个while 循环

while (Data.Length > 0)
{

//首先用substring 来获取每一个字符

sValue= Data.Substring(0, 1).ToString()

//然后将字符转换成ascii

sValue= Strings.Asc(sValue)

//然后将ascii值转换成十六进制

sValue = Conversion.Hex(sValue)

//转换后就去掉该字符

Data = Data.Substring(1, Data.Length - 1);

sHex = sHex + sValue;

}

Data_Hex_Asc(data)

这个函数负责将字符转换成十六进制

public string Data_Hex_Asc(ref string Data)

{

string Data1 = "";

string sData = "";

while (Data.Length > 0)

//first take two hex value using substring.

//then convert Hex value into ascii.

//then convert ascii value into character.

{
Data1 = System.
Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2),  16)).ToString();

sData = sData + Data1;

Data = Data.Substring(2, Data.Length - 2);

}

return sData;
}

Data_Asc_Hex(data)

这个函数负责将十六进制转换成字符

public string Data_Asc_Hex(ref string Data)
{

//first take each charcter using substring.
//then convert character into ascii.
//then convert ascii value into Hex Format

string sValue;

string sHex = "";

while (Data.Length > 0)
{
sValue =
Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()));
Data = Data.Substring(1, Data.Length - 1);
sHex = sHex + sValue;
}

return sHex;
}

posted @ 2007-04-14 00:36  T.t.T!Ck.￠#  阅读(17269)  评论(0编辑  收藏  举报