How Returns the number (hexa code point) corresponding to UTF-8 character (function)

GOALARG

New Member
Joined
Jul 3, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
Hi!

I have a Text in UTF-8, example
ÉDER MILITÃO and correct hexa code is: c389444552204d494c4954c3834f

Via function UNICODE(text) it returns 1 character with 1 byte only

É = 201 = C9

but my correct UTF-8 character is
É = C389

How can I get UTF-8 hexa valor?

look this page ,Convert UTF8 to Hexadecimal - Online UTF8 Tools

I need something as that

Any idea, thanks!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
The Unicode codepoint for É is actually U+00C9, not U+0C389.

U+0C389 is its UTF-8 encoded hexadecimal value.

So, basically, we have the following...

Character​
Binary code point​
Binary UTF-8​
Hex UTF-8​
U+00C9​
1100 1001​
1100 0011 1000 1001​
03 89​

Using your same website, here's the link that converts the Unicode character to the code point in hexadecimal...


And here's the link that converts the UTF-8 encoded character to a hexadecimal...


Have a look at the following link for a detailed explanation on UTF-8 encoding...


Hope this helps!
 
Upvote 0
Via function UNICODE(text) it returns 1 character with 1 byte only
This UDF, UTF8toHex, returns multiple bytes as a hex string.

VBA Code:
Option Explicit

#If VBA7 Then
    'Maps a UTF-16 (wide character) string to a byte array
    Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" _
        (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, ByVal lpMultiByteStr As LongPtr, ByVal cbMultiByte As Long, ByVal lpDefaultChar As LongPtr, ByVal lpUsedDefaultChar As LongPtr) As Long
#Else
    Private Declare Function WideCharToMultiByte Lib "kernel32" _
        (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
#End If

'UTF-8 code page
Private Const CP_UTF8 = 65001


Public Function UTF8toHex(UTF8string As String) As String
    Dim bytes() As Byte, i As Long
    UTF8toHex = ""
    bytes = Utf8StringToBytes(UTF8string)
    For i = 0 To UBound(bytes)
        UTF8toHex = UTF8toHex & Right("0" & Hex(bytes(i)), 2)
    Next
End Function

'Return byte array of VBA Unicode string encoded in UTF-8
Private Function UTF8StringToBytes(strInput As String) As Byte()

    Dim nBytes As Long
    Dim bytes() As Byte
    
    'Catch null input string
    If strInput = vbNullString Then
        UTF8StringToBytes = vbNullString
    Else
        'Get length in bytes
        nBytes = WideCharToMultiByte(CP_UTF8, 0&, ByVal StrPtr(strInput), Len(strInput), 0&, 0&, 0&, 0&)
        'Allocate zero-based array to receive the bytes
        ReDim bytes(0 To nBytes - 1)
        'Map string to byte array
        nBytes = WideCharToMultiByte(CP_UTF8, 0&, ByVal StrPtr(strInput), Len(strInput), ByVal VarPtr(bytes(0)), nBytes, 0&, 0&)
        UTF8StringToBytes = bytes
    End If
    
End Function
1625408427264.png
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top