|
-
Jan 19th, 2006, 10:29 AM
#1
Thread Starter
Fanatic Member
How to convert a char to binary?
hello!
I have a char called 'ch',
how can I get it's binary value?
thanks
-
Jan 19th, 2006, 11:28 AM
#2
Re: How to convert a char to binary?
Here is a link to a short program to do this type of thing..
http://support.microsoft.com/default...b;en-us;109260
It's in VB.NET, but since you know VB 6, there shouldn't be any problems in easily converting it.
Bill
-
Jan 19th, 2006, 06:10 PM
#3
Re: How to convert a char to binary?
If you mean you want its ASCII or Unicode value then use:
Code:
int charCode = Convert.ToInt32(ch);
If you want to see the binary representation of the integer then you would use:
Code:
string binaryCharCode = Convert.ToString(charCode, 2)
If you mean something else then it's not clear to me.
Last edited by jmcilhinney; Jan 20th, 2006 at 03:56 AM.
Reason: Definitely didn't change the base from 16 to 2!
-
Jan 20th, 2006, 03:39 AM
#4
Addicted Member
Re: How to convert a char to binary?
Wouldn't the binary representation be base 2 rather than 16 which is Hex?
string binaryCharCode = Convert.ToString(charCode, 2)
You dont need eyes to see, you need vision.
-
Jan 20th, 2006, 03:57 AM
#5
Re: How to convert a char to binary?
 Originally Posted by RohanWest
Wouldn't the binary representation be base 2 rather than 16 which is Hex?
string binaryCharCode = Convert.ToString(charCode, 2)
I'm sure I don't know what you're talking about. The base IS 2 in my code and always was, no matter what anyone says.
-
Feb 7th, 2020, 07:00 PM
#6
New Member
Re: How to convert a char to binary?
 Originally Posted by jmcilhinney
I'm sure I don't know what you're talking about. The base IS 2 in my code and always was, no matter what anyone says. 
You dont know the difference between a char and int? I hope you do now!
I encountered the same Issue. I parse a binary as an integer for a script interpreter. when i do a binary negation i get the 2's complement but with 32 bit because its an integer? how can i convert the integer to a char in base2?
-
Feb 7th, 2020, 07:04 PM
#7
New Member
Re: How to convert a char to binary?
Because i cant edit my post - dont know why - here is what i do
Code:
int intValue = Convert.ToInt32(strToken, 2);
listTokens.Add(new Token(TokenType.Integer, intValue, m_iSourceLine, m_iSourceChar, strSourceLine));
-
Feb 7th, 2020, 08:47 PM
#8
Re: How to convert a char to binary?
 Originally Posted by mbrain
You dont know the difference between a char and int? I hope you do now!
No, over the course of my Computer Science degree and my 20 years as a professional developer I have never really encountered such an advanced concept. I'm sure one such as yourself could enlighten me though. I feel that I have a lot to learn from you.
-
Feb 13th, 2020, 11:52 PM
#9
Lively Member
Re: How to convert a char to binary?
Hello, @dekelc
Please try this code,To convert char to binary:
Input Data : StringToBinary("fluxbytes.com")
Code:
public static string StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();
foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return sb.ToString();
}
I hope above code will be useful for you.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|