Click here to Skip to main content
Rate this: bad
good
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.
 
Thanks!!
Posted 5-Aug-11 7:33am
Edited 8-Aug-11 7:52am
v4
Comments
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
good
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!
  Permalink  
Comments
Espen Harlinn at 8-Aug-11 16:34pm
   
Great!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
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.
--SA
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 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


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