Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Function that shows the Unicode code of a character

This is a discussion on Function that shows the Unicode code of a character within the Excel Questions forums, part of the Question Forums category; Does such function exist in Excel?...

  1. #1
    Board Regular
    Join Date
    Jan 2008
    Location
    Osaka, Japan
    Posts
    159

    Default Function that shows the Unicode code of a character

    Does such function exist in Excel?

  2. #2
    Board Regular
    Join Date
    Mar 2007
    Location
    Chicago Area
    Posts
    2,607

    Default Re: Function that shows the Unicode code of a character

    Does this help?

    1
    AB
    1a97
    297a
    Excel 2000

    Worksheet Formulas
    CellFormula
    B1=CODE(A1)
    B2=CHAR(A2)



    ..I think that is ascii code though, not what you are looking for?

  3. #3
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    15,249

    Default Re: Function that shows the Unicode code of a character

    Quote Originally Posted by Jubinell View Post
    Does such function exist in Excel?
    Hi Jubinell

    No. But you can use a simple udf to get it:

    Code:
    Function CodeUni(s As String, Optional bHex As Boolean = True) As Long
    CodeUni = IIf(bHex, Hex(AscW(Left(s, 1))), AscW(Left(s, 1)))
    End Function
    Assuming the character in A1, use the udf in a cell as:

    =CodeUni(a1)

    to get the hexadecimal value of the character (the usual value in unicode maps)

    or

    =CodeUni(a1,False)

    to get the decimal value of the character.

    If the first parameter of the udf is a string you get the code of its first character.
    Last edited by pgc01; Oct 15th, 2009 at 09:37 PM.
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  4. #4
    Board Regular
    Join Date
    Jan 2008
    Location
    Osaka, Japan
    Posts
    159

    Default Re: Function that shows the Unicode code of a character

    @taigovinda

    Thanks but no this is different. The function that I'm looking for would return some or all of the columns on the right at the following website:

    http://unicodelookup.com/#latin/1

    @pgc01

    Thanks I will try this out.




  5. #5
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    15,249

    Default Re: Function that shows the Unicode code of a character

    Sorry, remove the "As Long" from the procedure declaration:

    Code:
    Function CodeUni(s As String, Optional bHex As Boolean = True)
    CodeUni = IIf(bHex, Hex(AscW(Left(s, 1))), AscW(Left(s, 1)))
    End Function
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  6. #6
    Board Regular
    Join Date
    Jan 2008
    Location
    Osaka, Japan
    Posts
    159

    Default Re: Function that shows the Unicode code of a character

    @pgc01

    This seems to work flawlessly with one exception. Results starting with 0s were truncated in the output cell. For example the character 'A' returns 41 but its code should have been 0041.

    I tried to set the format of the cells as text before using the functions but with that the functions wouldn't evaluate at all.

    Any thoughts?

  7. #7
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    15,249

    Default Re: Function that shows the Unicode code of a character

    Notice that the "A" character number is 41 hexadecimal or 65 decimal. In maps you get 41 or 0041 or U+0041 or 0x41, but these are the same number with different formats, as you know left zeros in a number have no value.

    To get the hexadecimal numbers in the format 0000, try:

    Code:
    Function CodeUni(s As String, Optional bHex As Boolean = True)
    If bHex Then
        CodeUni = Right("0000" & Hex(AscW(Left(s, 1))), 4)
    Else
        CodeUni = AscW(Left(s, 1))
    End If
    End Function
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  8. #8
    Board Regular
    Join Date
    Jan 2008
    Location
    Osaka, Japan
    Posts
    159

    Default Re: Function that shows the Unicode code of a character

    I got you.
    Thanks!

  9. #9
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    15,249

    Default Re: Function that shows the Unicode code of a character

    I'll add also the inverse function so that this thread can be used as reference.

    Get the unicode character Γ (capital gamma) given its code we can use:

    =CharUni(393)
    =CharUni("393")
    =CharUni("0393")
    =CharUni(915,False)

    The second parameter has the value False if we give the code as decimal, True or omitted if the code is hexadecimal.

    Notice that if the hexadecimal code has letters the code must be entered as string or you may have a bad surprise.

    Ex:

    =CharUni(A93)
    =CharUni("A93")

    The first is the char corresponding to the value in A93, the second the char corresponding to the hexadecimal code "A93"

    Code:
    Function CharUni(sCode As String, Optional bHex As Boolean = True) As String
    If bHex Then CharUni = ChrW("&H" & sCode) Else CharUni = ChrW(sCode)
    End Function
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  10. #10
    Board Regular
    Join Date
    Jan 2008
    Location
    Osaka, Japan
    Posts
    159

    Default Re: Function that shows the Unicode code of a character

    Hi PGC,

    Sorry to open up an old post but I've encountered a problem with Unicode characters again.

    I'm looking at the character
    (that's an image)
    in text, it is
    ��

    Now when i run CodeUni(), I get D840.
    Yet when I run CharUni on D840, I get
    that's an image
    in text, it is:


    Any idea this is? This is a very rare sub-type of the Japanese radical hito (person). This character usually have 2 forms that are more common:, 人 and 亻. I do not have problems with these two forms.

    Thank you once again for any insight you can give.
    Last edited by Jubinell; Nov 3rd, 2009 at 11:23 PM.

Page 1 of 3 123 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com