'================> Used to encoding GSM message as UCS2
Public Shared Function UnicodeStr2HexStr(strMessage As [String]) As [String]
Dim ba As Byte() = Encoding.BigEndianUnicode.GetBytes(strMessage)
Dim strHex As [String] = BitConverter.ToString(ba)
strHex = strHex.Replace("-", "")
Return strHex
End Function
Public Shared Function HexStr2UnicodeStr(strHex As [String]) As [String]
Dim ba As Byte() = HexStr2HexBytes(strHex)
Return HexBytes2UnicodeStr(ba)
End Function
'================> Used to decoding GSM UCS2 message
Public Shared Function HexBytes2UnicodeStr(ba As Byte()) As [String]
Dim strMessage = Encoding.BigEndianUnicode.GetString(ba, 0, ba.Length)
Return strMessage
End Function
Public Shared Function HexStr2HexBytes(strHex As [String]) As Byte()
strHex = strHex.Replace(" ", "")
Dim nNumberChars As Integer = strHex.Length / 2
Dim aBytes As Byte() = New Byte(nNumberChars - 1) {}
Using sr = New StringReader(strHex)
Dim i As Integer = 0
While i < nNumberChars
aBytes(i) = Convert.ToByte(New [String](New Char(1) {DirectCast(ChrW(sr.Read()), Char), DirectCast(ChrW(sr.Read()), Char)}), 16)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Using
Return aBytes
End Function
Ghi nhớ sử dụng khi cần thiết