admin 管理员组

文章数量: 887031


2024年1月24日发(作者:reacttable组件)

c#中如何将十进制数的字符串转化成十六进制数的字符串

//

/// 把ã?字Á?符¤?串ä?转Áa换?成¨¦16进?制?

///

///

///

///

private string StringToHex(string str, Encoding enc)

{

byte[] bytes = es(str);

string result = ;

for (int i = 0; i < ; i++)

{

result += "%" + ng(bytes[i], 16);

}

return result;

}

///

/// 把ã?字Á?符¤?串ä?转Áa换?成¨¦16进?制?字Á?节¨²数ºy组Á¨¦

///

///

///

private byte[] StringToHexbyte(string str)

{

str = e(" ", "");

if (( % 2) != 0)

str += "";

byte[] bytes = new byte[ / 2];

for (int i = 0; i < ; i++)

{

bytes[i] = (ing(i * 2, 2), 16);

}

return bytes;

}

///

/// 将?字Á?节¨²数ºy组Á¨¦转Áa换?成¨¦16进?制?字Á?符¤?串ä?

///

///

///

private string ByteToHexString(byte[] bytes)

{

string str = ;

if (bytes != null)

{

for (int i = 0; i < ; i++)

{

str += bytes[i].ToString("X2");

}

}

return str;

}

///

/// 将?汉o字Á?转Áa换?成¨¦16进?制?

///

/// 汉o字Á?

/// 编À¨¤码?:êo如¨?“¡ãUDF-8,ê?gb2312”¡À

/// 是º?否¤?用®?„?,ê?‟¡¥号?隔?开a

///

private string ToHex(string str, string charset, bool fenge)

{

if (( % 2) != 0) //加¨®一°?个?空?格?字Á?符¤?

str += "";

Encoding enc = oding(charset); //编À¨¤码?类¤¨¤型¨ª

byte[] bytes = es(str); //转Áa换?成¨¦字Á?节¨²数ºy组Á¨¦

string result = ;

//逐e一°?转Áa换?

for (int i = 0; i < ; i++)

{

result += ("{0:X}", bytes[i]);

//是º?否¤?用®?逗o号?隔?开a

if (fenge && i != - 1)

{

result += ("{0}", ",");

}

}

return result;

}

///

/// 把ã?十º?六¢¨´进?制?转Áa换?成¨¦汉o字Á?

///

///

///

///

private string HexToString(string hex, string charset)

{

hex = e(" ", "");

hex = e(",", "");

hex = e("", "");

hex = e("n", "");

if (( %2) != 0)

{

hex += "20"; //加¨®一°?个?空?格?字Á?符¤?

}

byte[] bytes = new byte[ / 2];

for (int i = 0; i < ; i++)

{

//两¢?个?16进?制?的Ì?字Á?符¤?占?一°?个?字Á?节¨²

bytes[i] = (ing(i * 2, 2),ber) ;

}

Encoding enc = oding(charset); //指?定¡§编À¨¤码?

return ing(bytes); //把ã?字Á?节¨²数ºy组Á¨¦转Áa换?成¨¦

}


本文标签: 组件 作者