147 lines
5.2 KiB
VB.net
147 lines
5.2 KiB
VB.net
Public Class publicMode
|
||
'字符串转byte数组
|
||
Public Shared Function StringToByte(ByVal str As String) As Byte()
|
||
Return System.Text.Encoding.Default.GetBytes(str)
|
||
End Function
|
||
|
||
Public Shared Function StringToHexString(ByVal b As String) As String
|
||
Dim sbuff = StringToByte(b)
|
||
Return ByteToHex(sbuff)
|
||
End Function
|
||
|
||
'byte数组转字符串
|
||
Public Shared Function ByteToString(ByVal b As Byte()) As String
|
||
Return System.Text.Encoding.Default.GetString(b)
|
||
End Function
|
||
|
||
'byte数组转十六进制字符串
|
||
Public Shared Function ByteToHex(ByVal b As Byte()) As String
|
||
Dim s As String = ""
|
||
For Each c As Byte In b
|
||
s += c.ToString("X2")
|
||
Next
|
||
Return s
|
||
End Function
|
||
'字符串转十六进制字符串不足两位补0
|
||
Public Shared Function StringToHex(ByVal str As String) As String
|
||
Dim s As String = ""
|
||
For Each c As Char In str
|
||
s += Convert.ToString(Asc(c), 16).PadLeft(2, "0")
|
||
Next
|
||
Return s
|
||
End Function
|
||
'字符串每两个字节转十六进制字符串 不足两位补0
|
||
Public Shared Function StringToHex2(ByVal str As String) As String
|
||
Dim s As String = ""
|
||
Dim nv As String = ""
|
||
For i As Integer = 0 To str.Length - 1 Step 2
|
||
nv = str.Substring(i, 1)
|
||
If nv.Length = 1 Then
|
||
nv = "0" & nv
|
||
End If
|
||
s += nv
|
||
Next
|
||
Return s
|
||
End Function
|
||
|
||
|
||
|
||
Public Shared Function StringToHex3(ByVal str As String) As String
|
||
Dim result As String = ""
|
||
For Each c As Char In str
|
||
Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(c)
|
||
For Each b As Byte In bytes
|
||
result += b.ToString("X2")
|
||
Next
|
||
Next
|
||
Return result
|
||
End Function
|
||
'十六进制字符串转byte数组
|
||
Public Shared Function HexToByte(ByVal hex As String) As Byte()
|
||
Dim b As Byte() = New Byte(hex.Length / 2 - 1) {}
|
||
Try
|
||
For i As Integer = 0 To hex.Length - 1 Step 2
|
||
b(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
|
||
Next
|
||
Catch ex As Exception
|
||
Return Nothing
|
||
End Try
|
||
|
||
|
||
Return b
|
||
End Function
|
||
|
||
'十六进制字符串转字符串
|
||
Public Shared Function ByteToHexString(databuff() As Byte) As String
|
||
Dim strData As String = String.Empty
|
||
For i = 0 To databuff.Length - 1
|
||
strData &= $" {Hex(databuff(i)).PadLeft(2, "0"c)}"
|
||
Next
|
||
Return strData
|
||
End Function
|
||
'十六进制字符串转字符串
|
||
Public Shared Function HexStringToByte(hexstr As String) As Byte()
|
||
Dim result As Byte() = Nothing
|
||
'判断字符串是否为空或 者为空字符串
|
||
If String.IsNullOrEmpty(hexstr) Then Return Nothing
|
||
'替换字符中的分隔符号
|
||
hexstr = hexstr.Trim().Replace(" ", "").Replace("-", "").Replace(",", "").Replace(":", "").Replace(";", "").Replace(".", "")
|
||
'判断字符串的长度是否为偶数
|
||
If hexstr.Length Mod 2 <> 0 Then
|
||
'不为偶数往最后一个字节前面补0
|
||
hexstr = hexstr.Insert(hexstr.Length - 1, "0")
|
||
End If
|
||
'以2为步长,将字符串转换为字节数组 不足2位前面补0
|
||
Try
|
||
result = Enumerable.Range(0, hexstr.Length).Where(Function(x) x Mod 2 = 0).Select(Function(x) Convert.ToByte(hexstr.Substring(x, 2), 16)).ToArray()
|
||
Catch ex As Exception
|
||
result = Nothing
|
||
End Try
|
||
|
||
Return result
|
||
End Function
|
||
'Public Shared Function HexStringToByte(hexstr As String) As String
|
||
' '判断字符串是否为空或 者为空字符串
|
||
' If String.IsNullOrEmpty(hexstr) Then Return Nothing
|
||
' '替换字符中的分隔符号
|
||
' hexstr = hexstr.Trim().Replace(" ", "").Replace("-", "").Replace(",", "").Replace(":", "").Replace(";", "").Replace(".", "")
|
||
' '判断字符串的长度是否为偶数
|
||
' If hexstr.Length Mod 2 <> 0 Then
|
||
' '不为偶数往最后一个字节前面补0
|
||
' hexstr = hexstr.Insert(hexstr.Length - 1, "0")
|
||
' End If
|
||
' '以2为步长,将字符串转换为字节数组 不足2位前面补0
|
||
' Return hexstr 'Enumerable.Range(0, hexstr.Length).Where(Function(x) x Mod 2 = 0).Select(Function(x) Convert.ToByte(hexstr.Substring(x, 2), 16)).ToArray()
|
||
|
||
'End Function
|
||
|
||
''' <summary>
|
||
''' Int转2个字节Byte
|
||
''' 高字节在前,低字节在后
|
||
''' </summary>
|
||
''' <param name="i"></param>
|
||
''' <returns></returns>
|
||
Public Shared Function IntToByteHB(ByVal i As Integer) As Byte()
|
||
Dim btemp() As Byte = {0, 0}
|
||
Dim b() As Byte = BitConverter.GetBytes(i)
|
||
btemp(0) = b(0)
|
||
btemp(1) = b(1)
|
||
Return btemp
|
||
End Function
|
||
|
||
''' <summary>
|
||
''' Int转2个字节Byte
|
||
''' 高字节在前,低字节在后
|
||
''' </summary>
|
||
''' <param name="i"></param>
|
||
''' <returns></returns>
|
||
Public Shared Function IntToByteLB(ByVal i As Integer) As Byte()
|
||
Dim btemp() As Byte = {0, 0}
|
||
Dim b() As Byte = BitConverter.GetBytes(i)
|
||
btemp(0) = b(1)
|
||
btemp(1) = b(0)
|
||
Return btemp
|
||
End Function
|
||
|
||
End Class
|