解码已编码的字符串 [英] Decode an Encoded String

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

问题描述

我已经找了几天,显然我没有输入正确的搜索条件来找到我想要的东西,所以在此先感谢您的帮助.

我正在接收从外部数据源编码的字符串,因此无法更改接收数据的方式.我的问题是,我发现的所有示例总是谈论使用Encoder类将字符串编码为byte []格式,以及如何将byte []对象解码回字符串.

如果有人有解决方案或可以向我指出正确的方向,将不胜感激.

因此例如:
string strEncoded = "544d444e5036";
正如Espen指出的那样,这是一个十六进制字符串,因此我应该问的更好的问题是如何获取一个将十六进制数据加载到byte []中的字符串.我在下面发布了一个解决方案.

它应该解码为"TMDNP6"

它应该是utf-7或utf-8编码.

I have been looking for a couple days now and apparently I am not entering the correct search terms to find what I am looking for, so thank you in advance for any help.

I am receiving a string that is encoded from an external datasource so I can''t change how I''m receiving the data. My problem is that all the samples I find always talk about using the Encoder class to encode the string into byte[] format and how to decode a byte[] object back to a string.

If someone has a solution or can point me in the right direction it would be appreciated.

so for example:
string strEncoded = "544d444e5036";
As Espen points out this is a hex string so the better question I should have asked is how to get a string storing hex data loaded into a byte[]. I have posted a solution below.

It should decode to "TMDNP6"

It should be a utf-7 or utf-8 encode.

Thanks!!

推荐答案

string data = "544d444e5036";
byte[] tempArray = new byte[data.Length / 2];
for (int i = 0; i < tempArray.Length; i++)
{
   tempArray[i] = Convert.ToByte(data.Substring(i * 2, 2), 16);
}

string decode = Encoding.UTF7.GetString(tempArray);



Espen的建议和链接使我找到了一种我上面发布的解决方案.在此处找到的 [



Espen your suggestions and links lead me to find a solution like the one I''m posting above. Which was found here[^]

If anyone has any comments on the above let me know.

Thank you for your help!


这是扩展的ascii表: http: //www.commfront.com/ascii-chart-table.htm [ ^ ]

十六进制值T = 54
十六进制值M = 4D
D的十六进制值= 44
N = 4E的十六进制值
十六进制值P = 50
十六进制值6 = 36

使用 NumberStyles.HexNumber [ Byte.TryParse [ Encoding.ASCII.GetChars 转换的字节a> [^ ]

最好的问候
Espen Harlinn
Here is an extended ascii table:http://www.commfront.com/ascii-chart-table.htm[^]

Hex value of T=54
Hex value of M=4D
Hex value of D=44
Hex value of N=4E
Hex value of P=50
Hex value of 6=36

Convert pairs of charaters to bytes using NumberStyles.HexNumber[^] with the Byte.TryParse[^] method.

This will result in a byte[] array containing the bytes ready for conversion with the Encoding.ASCII.GetChars[^]

Best regards
Espen Harlinn


这篇关于解码已编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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