求转码中文域名的代码,或求把这个C#的翻译成VB.NET

KPBer 2012-06-27 01:14:59
如题,也就是中文域名转码为Punycode及反译的代码
我找了半天找到了个说是VB.NET的,但使用后发现那个是拿原C#的代码直接在线转换后贴出来的,这个代码直接在线C#转换VB.NET后问题很多,我不会C#看了半天又理解不了原义。
我找到了原C#的代码:
http://blog.jixin.ntxz.net/?p=1766
...全文
316 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-06-27
  • 打赏
  • 举报
回复
c#转出来也是这样的
KPBer 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
以上代码测试通过
Dim s As String
s = Domain.Text.Punycode.EncodingDomain("中国.香港")
Console.WriteLine(s)
Console.WriteLine(Domain.Text.Punycode.DecodingDomain(s))
[/Quote]
看了代码,vb.net的看懂了,这个本来就是有问题的,我再改一下,谢了!
KPBer 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
以上代码测试通过
Dim s As String
s = Domain.Text.Punycode.EncodingDomain("中国.香港")
Console.WriteLine(s)
Console.WriteLine(Domain.Text.Punycode.DecodingDomain(s))
[/Quote]
非常感谢!不过这个我测试纯中文域名没问题,但百度.com这样的域名就会不对了,正确应该是xn--wxtr44c.com,这个转出来是xn--wxtr44c.xn--com-,不知道是C#原版就这样还是转换时有什么问题?
孟子E章 2012-06-27
  • 打赏
  • 举报
回复
以上代码测试通过
Dim s As String
s = Domain.Text.Punycode.EncodingDomain("中国.香港")
Console.WriteLine(s)
Console.WriteLine(Domain.Text.Punycode.DecodingDomain(s))
KPBer 2012-06-27
  • 打赏
  • 举报
回复
不知道他这个意思是要用asc函数把char转为对应的asc码,还是要把一个整形和char型的量拼接成一个string型的值,如0a这种造型。
孟子E章 2012-06-27
  • 打赏
  • 举报
回复
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace Domain.Text
''' <summary>
''' Punycode IDN编码操作
''' </summary>
Public Class Punycode
' Punycode parameters

Shared TMIN As Integer = 1
Shared TMAX As Integer = 26
Shared BASE As Integer = 36
Shared INITIAL_N As Integer = 128
Shared INITIAL_BIAS As Integer = 72
Shared DAMP As Integer = 700
Shared SKEW As Integer = 38
Shared DELIMITER As Char = "-"c
Public Shared Function EncodingDomain(domain As String) As String
domain = domain.Replace("。", ".")
Dim domainArray As String() = domain.Split(New String() {"."}, StringSplitOptions.None)
Dim result As String = ""
For Each item As String In domainArray
If item = "" Then
result += "."
Continue For
End If
result += "xn--" & Encode(item) & "."
Next
If result(result.Length - 1) = "."c Then
result = result.Substring(0, result.Length - 1)
End If
Return result
End Function

Public Shared Function DecodingDomain(domain As String) As String
domain = domain.Replace("。", ".")
Dim domainArray As String() = domain.Split(New String() {"."}, StringSplitOptions.None)
Dim result As String = ""
For Each item As String In domainArray
If item = "" Then
result += "."
Continue For
End If
Dim item2 As String = item
If item2.Length > 4 AndAlso item2.Substring(0, 4) = "xn--" Then
result += Decode(item2.Substring(4, item2.Length - 4)) + "."
End If
Next
If result(result.Length - 1) = "." Then
result = result.Substring(0, result.Length - 1)
End If
Return result
End Function
Public Shared Function Encode(inputStr As String) As String
Dim n As Integer = INITIAL_N
Dim delta As Integer = 0
Dim bias As Integer = INITIAL_BIAS
Dim output As New StringBuilder()
' Copy all basic code points to the output
Dim b As Integer = 0
For i As Integer = 0 To inputStr.Length - 1
Dim c As Char = inputStr(i)
If isBasic(c) Then
output.Append(c)
b += 1
End If
Next
' Append delimiter
If b > 0 Then
output.Append(DELIMITER)
End If
Dim h As Integer = b
While h < inputStr.Length
Dim m As Integer = Integer.MaxValue
' Find the minimum code point >= n
For i As Integer = 0 To inputStr.Length - 1
Dim c As Integer = AscW(inputStr.Substring(i, 1))
If c >= n AndAlso c < m Then
m = c
End If
Next
If m - n > (Integer.MaxValue - delta) \ (h + 1) Then
Throw New Exception("OVERFLOW")
End If
delta = delta + (m - n) * (h + 1)
n = m
For j As Integer = 0 To inputStr.Length - 1
Dim c As Integer = AscW(inputStr.Substring(j, 1))
If c < n Then
delta += 1
If 0 = delta Then
Throw New Exception("OVERFLOW")
End If
End If
If c = n Then
Dim q As Integer = delta
Dim k As Integer = BASE
While True
Dim t As Integer
If k <= bias Then
t = TMIN
ElseIf k >= bias + TMAX Then
t = TMAX
Else
t = k - bias
End If
If q < t Then
Exit While
End If
output.Append(ChrW(digit2codepoint(t + (q - t) Mod (BASE - t))))
q = (q - t) \ (BASE - t)
k += BASE
End While
output.Append(ChrW(digit2codepoint(q)))
bias = adapt(delta, h + 1, h = b)
delta = 0
h += 1
End If
Next
delta += 1
n += 1
End While
Return output.ToString()
End Function
Public Shared Function Decode(input As String) As String

Dim n As Integer = INITIAL_N
Dim i As Integer = 0
Dim bias As Integer = INITIAL_BIAS
Dim output As New StringBuilder()
Dim d As Integer = input.LastIndexOf(DELIMITER)

If d > 0 Then
For j As Integer = 0 To d - 1
Dim c As Char = input(j)
If Not isBasic(c) Then
Throw New Exception("BAD_INPUT")
End If
output.Append(c)
Next
d += 1
Else
d = 0
End If
While d < input.Length
Dim oldi As Integer = i
Dim w As Integer = 1
Dim k As Integer = BASE
While True
If d = input.Length Then
Throw New Exception("BAD_INPUT")
End If

Dim c As Integer = AscW(input.Substring(d, 1))
d = d + 1
Dim digit As Integer = codepoint2digit(c)
If digit > (Integer.MaxValue - i) \ w Then
Throw New Exception("OVERFLOW")
End If
i = i + digit * w
Dim t As Integer
If k <= bias Then
t = TMIN
ElseIf k >= bias + TMAX Then
t = TMAX
Else
t = k - bias
End If
If digit < t Then
Exit While
End If
w = w * (BASE - t)
k += BASE
End While
bias = adapt(i - oldi, output.Length + 1, oldi = 0)
If i \ (output.Length + 1) > Integer.MaxValue - n Then
Throw New Exception("OVERFLOW")
End If
n = n + i \ (output.Length + 1)
i = i Mod (output.Length + 1)
output.Insert(i, ChrW(n))
i += 1
End While
Return output.ToString()
End Function
Private Shared Function adapt(delta As Integer, numpoints As Integer, first As Boolean) As Integer
If first Then
delta = delta \ DAMP
Else
delta = delta \ 2
End If
delta = delta + (delta \ numpoints)
Dim k As Integer = 0
While delta > ((BASE - TMIN) * TMAX) \ 2
delta = delta \ (BASE - TMIN)
k = k + BASE
End While
Return k + ((BASE - TMIN + 1) * delta) \ (delta + SKEW)
End Function
Private Shared Function isBasic(c As Char) As Boolean
Return AscW(c) < &H80
End Function
Private Shared Function digit2codepoint(d As Integer) As Integer
If d < 26 Then
' 0..25 : 'a'..'z'
Return d + Asc("a"c)
ElseIf d < 36 Then
' 26..35 : '0'..'9';
Return d - 26 + Asc("0"c)
Else
Throw New Exception("BAD_INPUT")
End If
End Function
Private Shared Function codepoint2digit(c As Integer) As Integer
If c - Asc("0"c) < 10 Then
' '0'..'9' : 26..35
Return c - Asc("0"c) + 26
ElseIf c - Asc("a"c) < 26 Then
' 'a'..'z' : 0..25
Return c - Asc("a"c)
Else
Throw New Exception("BAD_INPUT")
End If
End Function
End Class
End Namespace
KPBer 2012-06-27
  • 打赏
  • 举报
回复
但是d是Integer的,对Integer型和char型的两个量运用一个运算符+又是什么意思呢?前面也有很多企图将char型转为Integer型而出错的地方。
iyomumx 2012-06-27
  • 打赏
  • 举报
回复
"a"c 是指字符'a',类型是Char
KPBer 2012-06-27
  • 打赏
  • 举报
回复
我在http://www.developerfusion.com/tools/convert/csharp-to-vb转的,这个转出来有很多问题,比如Return d + "a"c这样的语句等等,d是个变量,a是字符串,c放在这里就是个错误了,不懂C#,不知道原意是什么。
subMain 2012-06-27
  • 打赏
  • 举报
回复
http://www.developerfusion.com/tools/

http://www.developerfusion.com/tools/convert/csharp-to-vb/
subMain 2012-06-27
  • 打赏
  • 举报
回复
http://www.developerfusion.com/tools/convert/vb-to-csharp/

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧