Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Convert Unicode Value

2011-08-08
2013-01-25
  • This script, in answer to a question on the N++ Open Discussion forum, will convert a 4 digit (or less) unicode value to it's character equivalent.  For example, enter "20ac", then run the script (perhaps from a shortcut key), and it will replace it with the euro currency symbol.

    import string 
    def isHexDigit( char ): 
        result = ( char in string.hexdigits ) 
        return result 
        
    def isHexString( str ): 
        resultmap = map( isHexDigit, str ) 
        result = ( False not in resultmap ) 
        return result
    # Get the word nearest the cursor
    # Could use editor.getCurrentWord() for this,
    # but we want to be clever when the "word" is longer than 4 digits
    # If it is, then we take the position of the cursor as the end
    # If it's still longer, then we use the last 4 characters.
    wordStart = editor.wordStartPosition(editor.getCurrentPos(), True)
    wordEnd   = editor.wordEndPosition(editor.getCurrentPos(), True)
    if wordEnd - wordStart > 4:
        wordEnd = editor.getCurrentPos()
        if wordEnd - wordStart > 4:
            wordStart = wordEnd - 4
    
    code = editor.getTextRange(wordStart, wordEnd)
    # Check that the string is completely hex
    if isHexString(code):
        # Convert the code to a unicode string
        unicodeString = (r'\u' + ('0000' + code)[-4:]).decode('raw-unicode-escape')
    
        editor.setTarget(wordStart, wordEnd)
    
        # Paste the text in
        editor.replaceTarget(unicodeString.encode('utf8'))
        newPos = wordStart + len(unicodeString.encode('utf8'))
        editor.setAnchor(newPos)
        editor.setCurrentPos(newPos)