Punycode

          Property 
          Member of  ScriptUtils.ByteArray 

Description
Returns unicode String as punycode encoded string. You can directly convert data from/to any charset to punycode using this property.
Syntax
vPunycode = ByteArray.Punycode
ByteArray.Punycode = vPunycode
Returns
String
Remarks
Please use unicode code pages (utf-8 or utf-16, see ConvertCodePages) to work with all possible unicode characters.
See live sample of this method
Examples
 ByteArray - Punycode - unicode conversions  
Short functions to encode/decode string<->punycode data. You can use these functions as an Internationalized Domain Names VBScript converter.
  • punycode to unicode conversion
  • unicode string to punycode conversion
  • IDN string to unicode conversion
  • unicode string to IDN string conversion
    See Live punycode idn decoder/encoder.
  • Dim m_BA
    Function BA
      If IsEmpty(m_BA) Then Set m_BA = CreateObject("ScriptUtils.ByteArray"): m_BA.CharSet = "utf-8"
      Set BA = m_BA
    End Function
    
    'conversion from unicode string to punycode
    Function ToPUNYCODE(Data)
      Dim Outdata
      BA.String = Data
      
      Outdata =  "<div>ToPUNYCODE : Punycode representation of '" & _
       Data & "' string :<div style=background-color:yellow;color:blue>" 
      Outdata = Outdata & "<b>" & BA.Punycode & "</b>"
      Outdata = Outdata & "</div></div>"
      ToPUNYCODE = Outdata 
    End Function
    
    'conversion from punycode string (OLE String) to a UNICODE string
    Function FromPUNYCODE(Data)
      Dim Outdata
      On Error Resume Next
      BA.Punycode = Data
      If Err=0 Then
        Outdata =  "<div>FromPUNYCODE : Unicode string :<div style=background-color:yellow;color:blue>" 
        Outdata = Outdata & "<b>" & BA.String & "</b>"'write the UTF representation
        Outdata = Outdata & "</div></div>"
      Else
        Outdata =  "<div style=background-color:red;color:yellow>FromPUNYCODE:Punycode string '" & _
         Data & "' has no unicode representation.</div>" 
      End If
      FromPUNYCODE = Outdata 
    End Function
    
    'conversion of a host name (www.anychar.com) to a punycode idn version (www.xn--translated.com)
    Function ToIDN(Data)
      Dim Outdata, pData, partuni, partpuny
      
      pData = Split(Data, ".")
    
      For Each partuni In pData
        BA.String = partuni
        partpuny = BA.Punycode
        If Right(partpuny,1)<>"-" Then partpuny = "xn--" & partpuny Else partpuny = partuni
    
        Outdata = Outdata & partpuny & "."
      Next
      Outdata = Left(Outdata, Len(Outdata)-1)
      ToIDN =  "<div>ToIDN : IDN representation of '" & _
       Data & "' string :<div style=background-color:yellow;color:blue><b>" & _
       Outdata & "</b></div></div>"
    End Function
    
    'conversion of a host name in idn punycode (www.xn--translated.com) 
    ' to an unicode string (www.anychar.com)
    Function FromIDN(ByVal Data)
      Dim Outdata, pData, partuni, partpuny
      Data = LCase(Data)
      pData = Split(Data, ".")
      For Each partpuny In pData
        If Left(partpuny,4)="xn--" Then
          BA.Punycode = Mid(partpuny, 5)
          partuni = Ba.String
        Else
          'on error resume next
          'BA.Punycode = partpuny
          Dim re :set re = New RegExp
          re.pattern = "^[-a-zA-Z0-9]+$"
          
          If re.Test(partpuny) Then
            partuni = partpuny
          Else
            partuni = "<font Color=red>error (" & partpuny & ")</Font>"
          End If
        End If
    
        Outdata = Outdata & partuni & "."
      Next
      Outdata = Left(Outdata, Len(Outdata)-1)
      FromIDN = "<div>FromIDN : Unicode representation of IDN '" & Data & _
       "' :<div style=background-color:yellow;color:blue><b>" & _
       Outdata & "</b></div></div>"
    End Function
    See also
    Internationalized Domain Names VBScript converter, punycode to unicode conversion, converting IDN to UNICODE.
    This property is using
    VeriSign XCode (encode/decode) IDN Library
    A library for encoding / decoding of domain strings.
    (c) VeriSign Inc., 2003, All rights reserved 
    
    
      Other links for Punycode
    Methods of ByteArray class
    Properties of ByteArray class
    Works with safearray binary data - save/restore binary data from/to a disk, convert to a string/hexstring, codepage/charset conversions, Base64 conversion, etc.
         ByteArray is a COM class specially designed to work with Microsoft Windows Scripting engines - VB Script and JScript in Active Server Pages or WSH and in CHM or HTA applications. It also works with VB Net, Visual basic (VBA - VB 5, VB 6, Word, Excel, Access, …), C#, J#, C++, ASP, ASP.Net, Delphi and with T-SQL OLE functions - see Use ByteArray object article. You can also use the object in other programming environments with COM support, such is PowerBuilder.
         Source code for ByteArray is available within distribution license, please see License page for ScriptUtilities and Huge ASP file upload.
          Easy to use, hi-performance ASP file upload component with progress bar indicator. Let's you upload multiple files with size up to 2GB to a disk or database along with another form fields. Works with large posts, any character set (including unicode utf-8). Contains one-click multiple files/folders download with on-the-fly compression (Using BinaryWrite/BinaryRead).
          Hi-performance text file logging for ASP/VBScript/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.
           Lets you work with safearray binary data in VBS/JS. It also enables conversion between binary and String data using several code pages. ZLib compress and uncompress functions. Lets you transfer files using compressed stream from a client to server using IE.
          This library also enables calling of some Kernel and Advapi functions (performance, timing, sleep, configuration ...) and enables native work with INI files.


    © 1996 – 2005 Motobit Software, help{at}pstruh.cz, help v. 2.16.14