Android Question punycode/idn converter

trepdas

Active Member
Licensed 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
🙏
 

DonManfred

Expert
Licensed User
Longtime User
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
    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

mcqueccu

Well-Known Member
Licensed User
Longtime User
Thats a good one from @Erel

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


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

B4X:
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

trepdas

Active Member
Licensed User
Thats a good one from @Erel

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


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

B4X:
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
Top