vb与vb.net得到汉字ASC的函数
将vb写的函数改写成vb.net的函数,得到的结果不一样
vb的函数如下:
Private Sub PackBytes(ByteArray() As Byte, ByVal PostData As String)
Dim iNewBytes As Integer
Dim i As Integer, j As Integer, ch As String
Dim strHex As String
iNewBytes = LenB(StrConv(PostData, vbFromUnicode)) - 1
If iNewBytes < 0 Then Exit Sub
ReDim ByteArray(iNewBytes) As Byte
For i = 0 To Len(PostData) - 1
ch = Mid(PostData, i + 1, 1)
If ch = " " Then
ch = "+ "
ByteArray(j) = Asc(ch)
ElseIf Asc(ch) < 0 Then
ByteArray(j) = CByte( "&H " & Left(Hex(Asc(ch)), 2))
j = j + 1
ByteArray(j) = CByte( "&H " & Right(Hex(Asc(ch)), 2))
Else
ByteArray(j) = Asc(ch)
End If
j = j + 1
Next
End Sub
vb.net的函数如下:
Private Sub PackBytes(ByVal ByteArray() As Byte, ByVal PostData As String)
Dim iNewBytes As Integer
Dim i As Integer, j As Integer, ch As String
'Dim strHex As String
iNewBytes = System.Text.Encoding.Default.GetBytes(PostData).Length - 1
If iNewBytes < 0 Then Exit Sub
ReDim ByteArray(iNewBytes)
For i = 0 To Len(PostData) - 1
ch = Mid(PostData, i + 1, 1)
If ch = " " Then
ch = "+ "
ByteArray(j) = Asc(ch)
ElseIf Asc(ch) < 0 Then
ByteArray(j) = CByte( "&H " & Microsoft.VisualBasic.Left(Hex(Asc(ch)), 2))
j = j + 1
ByteArray(j) = CByte( "&H " & Microsoft.VisualBasic.Right(Hex(Asc(ch)), 2))
Else
ByteArray(j) = Asc(ch)
End If
j = j + 1
Next
End Sub
当我调用vb的函数,postDate传入“李”时,ByteArray里边的值为“192,238“
调用vb.net的函数,传入同样的参数,得到的值却为: "255,238 "
到底是什么原因呢?
[解决办法]
vb是asc
vb.net是unicode
[解决办法]
楼上正解
[解决办法]
搂猪,你引用Microsoft.VisualBasic命名空间 就可以使用vb6下的函数了,我当初也是从vb6转为vb.net,又转为C#
加这个一句:
Imports Microsoft.VisualBasic