Click here to Skip to main content
15,898,952 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to coding string to binary or hexidecimal code and in another place I want (in another window)comeback it to string .
Posted
Comments
Zoltán Zörgő 27-Jun-12 8:28am    
What is the exact purpose of this in your project?
Sandeep Mewara 27-Jun-12 9:46am    
Your issue?

Have a look at BinaryWriter[^] and BinaryReader[^]

They can be used with the MemoryStream[^] to facilitate reading and writing to binary formats.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Abhinav S 27-Jun-12 23:26pm    
Correct. A 5.
Espen Harlinn 28-Jun-12 7:03am    
Thank you, Abhinav!
Sandeep Mewara 28-Jun-12 9:03am    
Yep. 5!
Espen Harlinn 28-Jun-12 9:38am    
Thank you, Sandeep :-D
You probaby need base64 encoding.

C#
public string Encode(string str)
{
   byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
   return Convert.ToBase64String(encbuff);
}
public string Decode(string str)
{
   byte[] decbuff = Convert.FromBase64String(str);
   return System.Text.Encoding.UTF8.GetString(decbuff);
}
 
Share this answer
 
Comments
Espen Harlinn 27-Jun-12 18:55pm    
A fair guess :-)
Hi,
Try this-
C#
string str = "Hello";
byte []arr = System.Text.Encoding.ASCII.GetBytes(str);

Or
C#
string result = string.Empty;
foreach(char ch in yourString)
{
   result += Convert.ToString((int)ch,2);
}

OUTPUT-
this will translate "Hello" to 10010001100101110110011011001101111

String to Hex and vise versa

C#
public string ConvertStringToHex(string asciiString)
{
    string hex = "";
    foreach (char c in asciiString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}

public string ConvertHexToString(string HexValue)
{
    string StrValue = "";
    while (HexValue.Length > 0)
    {
        StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
        HexValue = HexValue.Substring(2, HexValue.Length - 2);
    }
    return StrValue;
}
 
Share this answer
 
v3
Comments
Espen Harlinn 27-Jun-12 18:53pm    
Nice reply :-D
Jαved 28-Jun-12 2:10am    
Thanks.
Member 12369400 8-Mar-16 7:57am    
how to convert same thing in java

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