Android Question punycode/idn converter

trepdas

Active Member
Licensed User
Longtime User
Hello Erel and all Good People,

I need a function that can convert one string to another.
punycode (i.e. : 'שלום' to idn ( i.e. : 'xn--9dbne9b')
and vise verse - idn to punycode.

an online converter (for the example) can be found here.

Punycode is a special encoding used to convert Unicode characters to ASCII, which is a smaller, restricted character set. Punycode is used to encode internationalized domain names (IDN).

thanks
?
 
an online converter (for the example) can be found here.
1. No link
2. You need to write a wrapper with java for it.
3. Alternatively you can create a thread in the JobOffer Forum and hire someone who do the job for you.
 
Upvote 0
B4X:
Expand Collapse Copy
    Dim s As String = ToACE("שלום")
    Log(s)
    Log(FromACE(s))

Sub ToACE (s As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("java.net.IDN")
    Return jo.RunMethod("toASCII", Array(s))
End Sub

Sub FromACE (s As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("java.net.IDN")
    Return jo.RunMethod("toUnicode", Array(s))
End Sub
 
Upvote 0
Thats a good one from @Erel

I also had time to scrap punycode.com website for you using minihtmlparser


Punycode Encoder Usage:
Expand Collapse Copy
Wait For (punycodeEncode("שלום")) Complete(result As String)
    Log(result)

B4X:
Expand Collapse Copy
Sub punycodeEncode(text As String) As ResumableSub
    Dim url As String = "https://www.punycoder.com/ajax-calls/punycoder/"
    Dim ok As HttpJob
    
    Dim data As String = $"submitButton=encode&Decoded=${text}"$
    
    ok.Initialize("ok",Me)
    ok.PostString(url,data)
    ok.GetRequest.SetHeader("Referer", "https://www.punycoder.com/")
    
    Wait For Jobdone (ok As HttpJob)
    
    If ok.Success Then
        Dim res As String
        res = ok.GetString
        
        '-----------------process result-------------------
        Dim parser As MiniHtmlParser
        parser.Initialize
        Dim root As HtmlNode = parser.Parse(res)
        Dim encoded_id As HtmlNode = parser.FindNode(root,"textarea",parser.CreateHtmlAttribute("id","Encoded"))
        Dim result As String = parser.GetTextFromNode(encoded_id,0)   
        
    End If
    ok.Release
    
    Return result
    
End Sub
 
Upvote 0
Thats a good one from @Erel

I also had time to scrap punycode.com website for you using minihtmlparser


Punycode Encoder Usage:
Expand Collapse Copy
Wait For (punycodeEncode("שלום")) Complete(result As String)
    Log(result)

B4X:
Expand Collapse Copy
Sub punycodeEncode(text As String) As ResumableSub
    Dim url As String = "https://www.punycoder.com/ajax-calls/punycoder/"
    Dim ok As HttpJob
   
    Dim data As String = $"submitButton=encode&Decoded=${text}"$
   
    ok.Initialize("ok",Me)
    ok.PostString(url,data)
    ok.GetRequest.SetHeader("Referer", "https://www.punycoder.com/")
   
    Wait For Jobdone (ok As HttpJob)
   
    If ok.Success Then
        Dim res As String
        res = ok.GetString
       
        '-----------------process result-------------------
        Dim parser As MiniHtmlParser
        parser.Initialize
        Dim root As HtmlNode = parser.Parse(res)
        Dim encoded_id As HtmlNode = parser.FindNode(root,"textarea",parser.CreateHtmlAttribute("id","Encoded"))
        Dim result As String = parser.GetTextFromNode(encoded_id,0)  
       
    End If
    ok.Release
   
    Return result
   
End Sub
thank you for this! will save it as good example for future use
 
Upvote 0
Back
Top