从字符串ascii转换为字符串Hex [英] Convert from string ascii to string Hex

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

问题描述

假设我有这个字符串

string str = "1234"

我需要一个将此字符串转换为此字符串的函数:

I need a function that convert this string to this string:

"0x31 0x32 0x33 0x34"  

我在网上搜索,发现很多类似的东西,但不是这个问题的答案。

I searched online and found a lot of similar things, but not an answer to this question.

推荐答案

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 

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

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