Click here to Skip to main content
15,868,066 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All

I have to convert a string value to binary .
can someone let me know how to do it.

I tried this
C#
byte[] EmployeeId = System.Text.Encoding.ASCII.GetBytes(comboBox1.SelectedItem.ToString());


Please can someone let me know the right way .
Conversion value should show in this way (0x0000000000000733).


Many Thanks

suman
Posted
Updated 1-Sep-14 4:04am
v2
Comments
Richard Deeming 1-Sep-14 10:06am    
What is the value of the selected item that you're trying to convert?
babli3 1-Sep-14 10:08am    
It is a string . I want that string to be converted as binary
Richard Deeming 1-Sep-14 10:11am    
I know it's a string; what is the value of the string? What characters does it contain?
[no name] 1-Sep-14 10:14am    
You would need to pad your string and 0x0000000000000733 is not binary.
babli3 1-Sep-14 10:13am    
My combo box has all employee names (a-z).Thanks

1 solution

Did you test your code ?


C#
string str =comboBox1.SelectedItem.ToString();
byte[] arr = System.Text.Encoding.ASCII.GetBytes(str);


// to check or debug your output 
string result = string.Empty;

foreach(char ch in str)
{
   result += Convert.ToString((int)ch,2);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Sep-14 12:54pm    
This is totally unrelated to OP's "problem" (which is not a problem at all; the OP's problem is just understanding).
Why doing all those manipulations you show here? It makes no sense.
—SA
Marc Koutzarov 1-Sep-14 13:04pm    
guess i'm missing the the point then.

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