字符串转换"1234ABC";变成"F1F2F3F4C1C2C3"; [英] String conversion "1234ABC" into "F1F2F3F4C1C2C3"
本文介绍了字符串转换"1234ABC";变成"F1F2F3F4C1C2C3";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我是c#编程的新手,需要将字符串"1234ABCD"转换为"F1F2F3F4C1C2C3C4".请提出一些好的方法.
Thnx,
adir
Hi All, I am new in c# programming and need to convert a string "1234ABCD" into "F1F2F3F4C1C2C3C4".Could you please suggest some good way for this.
Thnx,
adir
推荐答案
类似此 [ ^ ].
用他们的方式,必须猜测这就是您想要的:您的问题应该确实是如何将ASCII字符串转换为十六进制?".您还应该学习使用Google.
Something like this[^].
By they way, had to guess that this is what you wanted: your question should really have been ''How do I convert an ASCII string to Hex?''. You should also learn to use Google.
首先,您使用了错误的代码类型.您要转换的是EBCDIC中的表示形式( http://en.wikipedia.org/wiki/EBCDIC [ ^ ]),而不是ASCII(http://en.wikipedia.org/wiki/ASCII [ ^ ]),Microsoft和其他大多数系统上都使用过.我提供的代码将只转换前8位,而不转换全部16位,但是如果您要这样做,可以将其固定为:
First of all, you have the wrong code type. What you are converting to is the representations in EBCDIC (http://en.wikipedia.org/wiki/EBCDIC[^]), not ASCII (http://en.wikipedia.org/wiki/ASCII[^]) which is used by Microsoft, and on most other systems. The code I am providing will only convert the first 8 bits, not the full 16 bits, but can be fixed to do that if that is what you want:
string testString = "1234ABCD";
var stringBuilder = new StringBuilder();
foreach (char chr in testString)
{
var lft = ((int) chr & 240) / 16;
stringBuilder.Append(ConvertNibbleToHex(lft));
var rt = (int) chr & 15;
stringBuilder.Append(ConvertNibbleToHex(rt));
}
它使用以下功能:
This uses the following function:
private static char ConvertNibbleToHex(int value)
{
if (value < 10)
return (char) (value + 48);
return (char)(value + 55);
}
这篇关于字符串转换"1234ABC";变成"F1F2F3F4C1C2C3";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文