将换行从字符串转换为十六进制 [英] Convert Line Feed from string to hex

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

问题描述



我正在使用以下代码将字符串转换为十六进制:

Hi,

I''m using the following code to convert a string to hex:

char[] charArray = plainText.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int num;
            string hex;
            for (int i = 0; i < charArray.Length; i++)
            {
                num = Convert.ToInt32(charArray[i]);
                hex = num.ToString("x");
                sb.Append(hex);
            }


这里的问题是,此代码将所有换行都转换为DA而不是0A.根据ASCII表以十六进制表示:

DA-Ú
0A-换行符

有人知道如何正确转换换行符吗?谢谢,努诺.


The problem here is that, this code is converting all the line feeds into DA instead of 0A. According to the ASCII table to HEX:

DA - Ú
0A - linefeed

Does anyone got an idea about how can I convert the line feeds correctly?? Thanks, Nuno.

推荐答案

不,不是:仔细观察,您会看到它实际上输出两个值:"D"表示回车符和换行符为"A".
为了证明这一点,请在每个sb.Append上添加一个逗号.
No it isn''t: Look more closely and you will see it is actually outputting two values: "D" for Carriage return and "A" for Line feed.
To prove it, add a comma to each sb.Append.



这可能是格式问题,您看到的是回车符(0xD),后跟换行符(0xA).当num< 0x10 ToString("x")将仅输出一个字符. "x2"将确保在输出中填充前导零,长度为2.

数字格式的字符串 [
Hi,
This is probably a formatting issue and what you are seeing is a carriage return (0xD) followed by a line feed(0xA). When num is < 0x10 ToString("x") will output only one character. "x2" will ensure that the output is padded with leading zeroes to a length of 2.

Numeric format strings[^]

Alan.


事实上,在ToString方法中使用"x2"可以正确转换字符串.

谢谢!
It is in fact true that using "x2" in the ToString method, converts the string correctly.

Thanks!


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

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