c# - 从字符串 ascii 转换为字符串 Hex

标签 c# string hex ascii

假设我有这个字符串

string str = "1234"

我需要一个将这个字符串转换成这个字符串的函数:

"0x31 0x32 0x33 0x34"  

网上查了很多类似的东西,都没有这个问题的答案。

最佳答案

string str = "1234";
char[] charValues = str.ToCharArray();
string hexOutput="";
foreach (char _eachChar in charValues )
{
    // Get the integral value of the character.
    int value = Convert.ToInt32(_eachChar);
    // Convert the decimal value to a hexadecimal value in string form.
    hexOutput += String.Format("{0:X}", value);
    // to make output as your eg 
    //  hexOutput +=" "+ String.Format("{0:X}", value);

}

    //here is the HEX hexOutput 
    //use hexOutput 

关于c# - 从字符串 ascii 转换为字符串 Hex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920741/

相关文章:

c - strtok 不只在指定的分隔符上标记

python - python中不可打印的字符串到数字

javascript - Three.js:将RGB十进制颜色转换为HEX RGB颜色

python - 在 Python 中将十六进制字符串表示形式转换为实际字节

c# - 如何创建类的实例并注入(inject)服务?

C# + 信息权限管理

c# - 获取 5 的乘积范围

c# - DynamicRouteValueTransformer 不存在

javascript - 使用JQuery实时大写用户输入的第一个字符

python - Tensorflow:将张量作为一个整体进行字符串化(不创建字符串张量)