MSDN将字节数组转换为十六进制值字符串时 Byte 的用法
2007-08-19 22:50
901 查看
示例
[Visual Basic, C#] 下面的示例说明将字节数组转换为十六进制值字符串时 Byte 的用法。
[Visual Basic]
Class HexTest
Private Shared hexDigits As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c,
"8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}
Public Shared Function ToHexString(bytes() As Byte) As String
Dim hexStr As String = ""
Dim i As Integer
For i = 0 To bytes.Length - 1
hexStr = hexStr + Hex(bytes(i))
Next i
Return hexStr
End Function 'ToHexString
Shared Sub Main()
Dim b As Byte() = {&H0, &H12, &H34, &H56, &HAA, &H55, &HFF}
Console.WriteLine(ToHexString(b))
End Sub 'Main
End Class 'HexTest
[C#]
class HexTest
{
static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static string ToHexString(byte[] bytes)
{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
static void Main()
{
byte[] b = { 0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF };
Console.WriteLine(ToHexString(b));
}
}
[Visual Basic, C#] 下面的示例说明将字节数组转换为十六进制值字符串时 Byte 的用法。
[Visual Basic]
Class HexTest
Private Shared hexDigits As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c,
"8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}
Public Shared Function ToHexString(bytes() As Byte) As String
Dim hexStr As String = ""
Dim i As Integer
For i = 0 To bytes.Length - 1
hexStr = hexStr + Hex(bytes(i))
Next i
Return hexStr
End Function 'ToHexString
Shared Sub Main()
Dim b As Byte() = {&H0, &H12, &H34, &H56, &HAA, &H55, &HFF}
Console.WriteLine(ToHexString(b))
End Sub 'Main
End Class 'HexTest
[C#]
class HexTest
{
static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static string ToHexString(byte[] bytes)
{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
static void Main()
{
byte[] b = { 0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF };
Console.WriteLine(ToHexString(b));
}
}
相关文章推荐
- MSDN将字节数组转换为十六进制值字符串时 Byte 的用法
- 将字节数组转换为十六进制值字符串时 Byte 的用法
- C#中字节数组(byte[])和字符串相互转换
- 如何将字节数组转换为十六进制的字符串
- 字节数组和字符串按十六进制互相转换
- 随手在网上找了一段将字节数组转换为十六进制字符串的代码,结果被坑惨了
- Java整数-字节,字节-字符,字节数组-十六进制字符串转换
- 字节数组、数值和十六进制字符串的转换
- C int和BYTE互转、字符串转十六进制字节数组
- byte[]数组与十六进制字符串与字符串的互相转换(转载)
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
- 随手在网上找了一段将字节数组转换为十六进制字符串的代码,结果被坑惨了
- MD5加密与byte[]数组与十六进制字符串与字符串的互相转换
- java 中字节数组与十六进制字符串及普通字符串之间的转换
- JavaScript十六进制字符串和字节数组相互转换
- 字符串String转换成字节数组 byte[]的表现形式
- java中byte数组与十六进制字符串相互转换
- 字节数组、数值和十六进制字符串的转换
- 字节数组、数值和十六进制字符串的转换
- 使用Java完成byte数组与十六进制字符串相互转换