Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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!!
Posted
Updated 8-Aug-11 6:52am
v4
Comments
walterhevedeich 5-Aug-11 12:34pm    
Do you know or have the code of how the string was encoded?
jetskij16 5-Aug-11 12:52pm    
utf-8 or utf-7 this one is 7 but the encoding varies from them.

C#
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 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!
 
Share this answer
 
Comments
Espen Harlinn 8-Aug-11 16:34pm    
Great!
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
 
Share this answer
 
Comments
walterhevedeich 5-Aug-11 12:56pm    
Very good advice. My 5.
Espen Harlinn 5-Aug-11 13:01pm    
Thanks Walter :)
RaisKazi 5-Aug-11 13:38pm    
My 5!
Espen Harlinn 5-Aug-11 13:40pm    
Thank you, RaisKazi!
thatraja 5-Aug-11 21:47pm    
Simple & Nice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900