Click here to Skip to main content
11,705,052 members (52,580 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# string binary
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
Edited 8-Aug-11 6:52am
walterhevedeich at 5-Aug-11 12:34pm
Do you know or have the code of how the string was encoded?
jetskij16 at 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 at 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 at 5-Aug-11 12:56pm
Very good advice. My 5.
Espen Harlinn at 5-Aug-11 13:01pm
Thanks Walter :)
RaisKazi at 5-Aug-11 13:38pm
My 5!
Espen Harlinn at 5-Aug-11 13:40pm
Thank you, RaisKazi!
thatraja at 5-Aug-11 21:47pm
Simple & Nice
Espen Harlinn at 6-Aug-11 5:40am
Thank you, thatraja!
SAKryukov at 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 at 6-Aug-11 5:40am
Thanks Sergey!
Kim Togo at 8-Aug-11 15:39pm
My 5 Espen. Nice.
Espen Harlinn at 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
0 Sergey Alexandrovich Kryukov 609
1 OriginalGriff 439
2 Andy Lanng 215
3 CPallini 202
4 F-ES Sitecore 160
0 OriginalGriff 8,958
1 Sergey Alexandrovich Kryukov 8,276
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 8 Aug 2011
Copyright © CodeProject, 1999-2015
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