如何将Utf8字符转换为hexa [英] How do I convert Utf8 characters to hexa

查看:95
本文介绍了如何将Utf8字符转换为hexa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个将十六进制转换为utf-8字符的程序。程序转换所有字符并结束;到utf8。但我想要的是反之亦然。



我不知道如何找到所有 uft8字符例如.Éù)一个xml文件并将其转换为十六进制Éú

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;)

推荐答案

基本上,你有两个函数来获取一个字节数组(你称之为'十六进制字符')到一个字符串,反之亦然:

Basically, you have two functions to get a byte array (what you call 'hexadecimal characters') to a string, and vice-versa:
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:

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.


这篇关于如何将Utf8字符转换为hexa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆