C#语言如何将string格式的有0x开头的16进制整数转换为byte的格式?

2024-11-12 01:39:20
推荐回答(5个)
回答1:

利用System.Convert.ToByte方法可以将 0x开始的十六进制格式字符串转换成byte类型值。例如

string s = "0x55";
byte b = System.Convert.ToByte(s, 16);
Console.WriteLine(b);  //输出85

s = "0xff";
b = System.Convert.ToByte(s, 16);
Console.WriteLine(b);  //输出255

即使字符串中不包含前导"0x",System.Convert.ToByte也可完成转换

string s = "55"; //55是十六进制格式
byte b = System.Convert.ToByte(s, 16);
Console.WriteLine(b);  //输出85

s = "ff";      //ff是十六进制格式
b = System.Convert.ToByte(s, 16);
Console.WriteLine(b);  //输出255

为了防止转换过程的异常,可以用try...catch...

s = ....;
byte b = 0;
try
{
   b = System.Convert.ToByte(s, 16);  
}
catch(FormatException)
{
    Console.WriteLine("无法转换成byte类型值!");
}

回答2:

其他的部分,你可以自己修改一下!
///
///
Summary
description
for
StrHelper.
///
命名缩写:
///
Str:
unicode
string
///
Arr:
unicode
array
///
Hex:
二进制数据
///
Hexbin:
二进制数据用ASCII字符表示

字符''1''的hex是0x31表示为hexbin是
''3''''1''
///
Asc:
ASCII
///
Uni:
UNICODE
///
public
sealed
class
StrHelper
{
#region
Hex与Hexbin的转换
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
#region
数组和字符串之间的转化
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
}

回答3:

byte b = Byte.Parse("1D",System.Globalization.NumberStyle.HexNumber)
不需要"0x"了

你在转制前把0x去掉,转回来的时候把它加上不就完了么

回答4:

string value = original.Replace("0x", string.Empty);
byte b = Byte.Parse(value,System.Globalization.NumberStyle.HexNumber)
这样就可以了

c#的数据格式转换通常都是调用对应类的parse方法

回答5:

其他的部分,你可以自己修改一下!
///


/// Summary description for StrHelper.
/// 命名缩写:
/// Str: unicode string
/// Arr: unicode array
/// Hex: 二进制数据
/// Hexbin: 二进制数据用ASCII字符表示 例 字符''1''的hex是0x31表示为hexbin是 ''3''''1''
/// Asc: ASCII
/// Uni: UNICODE
///

public sealed class StrHelper
{
#region Hex与Hexbin的转换
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

#region 数组和字符串之间的转化
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
}