Click here to Skip to main content
12,999,628 members (60,861 online)
Rate this:
Please Sign up or sign in to 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.

Posted 5-Aug-11 6:33am
Updated 8-Aug-11 6:52am
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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!
Espen Harlinn 8-Aug-11 16:34pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is an extended ascii table:[^]

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
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
Espen Harlinn 6-Aug-11 5:40am
Thank you, thatraja!
SAKryukov 6-Aug-11 3:39am
Simple and nice, my 5, yes, but I have no idea how it may make any sense -- but this is OP's problem. I would not try to answer though.
Espen Harlinn 6-Aug-11 5:40am
Thanks Sergey!
Kim Togo 8-Aug-11 15:39pm
My 5 Espen. Nice.
Espen Harlinn 8-Aug-11 16:35pm
Thank you, Kim!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 8 Aug 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100