Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I already created a program that will convert hexadecimal to utf-8 characters. Program convert all the characters "" and ends ";" to utf8. But what I want is the vice-versa of this.

I don't know how will I find all the uft8 character (eg. É ù) in a xml file and convert this to hexadecimal (& #x00C9; & #x00FA;)
Posted
Comments
Sergey Alexandrovich Kryukov 21-Oct-15 15:04pm     CRLF
It depends on what you want to achieve: 1) bytes variable-length words representing characters in UTF8? 2) Unicode code point values represented by UTF8? 3) anything else? —SA
[no name] 21-Oct-15 16:56pm    
Your question and the accepted answer do not really match...

1 solution

Basically, you have two functions to get a byte array (what you call 'hexadecimal characters') to a string, and vice-versa:
C#
using System.Text;

// Converting an UTF8 string to a byte array
string input = "whatever";
byte[] bytes = Encoding.UTF8.GetBytes(input);

// Converting a byte array back to its original string representation
string result = Encoding.UTF8.GetString(bytes);


Once you have a byte array, the task to display it in its hexadecimal form is trivial. Here's an example implementation in an extension method:
C#
using System.Text;

public static string ToHexadecimalRepresentation(this byte[] bytes) {
   StringBuilder sb = new StringBuilder(bytes.Length << 1);
   foreach (byte b in bytes) {
      sb.AppendFormat("{0:X2}", b);
   }
   return sb.ToString();
}

Hope this helps.
 
Share this answer
 
v3
Comments
BillWoodruff 21-Oct-15 12:37pm    
+5
phil.o 21-Oct-15 14:04pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900