Results 1 to 9 of 9

Thread: How to convert a char to binary?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question How to convert a char to binary?

    hello!

    I have a char called 'ch',
    how can I get it's binary value?

    thanks

    Dekel C.

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert a char to binary?

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    New Member
    Join Date
    Feb 2020
    Posts
    2

    Re: How to convert a char to binary?

    Quote Originally Posted by jmcilhinney View Post
    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
    Code:
    ~
    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?

  7. #7
    New Member
    Join Date
    Feb 2020
    Posts
    2

    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));

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to convert a char to binary?

    Quote Originally Posted by mbrain View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Lively Member
    Join Date
    Jan 2020
    Posts
    119

    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
  •  



Click Here to Expand Forum to Full Width