CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    5

    Display an int in Binary

    Hi ,

    This might be simple , i just couldn't find out how i do it easily.

    I have an int variable , i want to convert into string in binary format

    ex.

    int number1= 3;

    i need the variable (a string) number_binary have the value "00000011" ( with 8 digit with leading zeros.)

    where number1 <=127 so range is not a problem.

    and , as you could probably guess , i need to some what modified the binary value and turn it back to an int , is there any easy way doing this?

  2. #2
    Join Date
    Nov 2008
    Location
    United States
    Posts
    81

    Re: Display an int in Binary

    Last edited by Three5Eight; December 13th, 2008 at 03:24 PM.
    Three5Eight
    Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Display an int in Binary

    The result is simple like
    Code:
    int a = 35;
    // converting to a string showing binary digits
    string result = Convert.ToString(a, 2).PadLeft(8, '0');
    // back from a binary sigits string to an integer
    int b = Convert.ToInt32(result, 2);
    Thats all
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4

    Re: Display an int in Binary

    Quote Originally Posted by siulung View Post
    Hi ,

    This might be simple , i just couldn't find out how i do it easily.

    I have an int variable , i want to convert into string in binary format

    ex.

    int number1= 3;

    i need the variable (a string) number_binary have the value "00000011" ( with 8 digit with leading zeros.)

    where number1 <=127 so range is not a problem.

    and , as you could probably guess , i need to some what modified the binary value and turn it back to an int , is there any easy way doing this?
    Safer and smaller way to store and convert these values in case number1 > 8 bits

    Code:
    // Why use int when you know the number will not ever exceed an 8 bit value, use byte or sbyte instead.
    
    sbyte number1 = 3;
    
    string binary8 = Convert.ToString(number1, 2).PadLeft(8, '0');
    
    // Convert Binary String Back to signed 8 bits
    // Safe way to make sure value never exceeds 8 bit value
    
    sbyte number1 = Convert.Convert.ToSByte(binary8, 2);
    
    //Display sbyte from 8 Bit Binary String
    MessageBox.Show("New sbyte:" + number1 + " 8 Bit Binary String:" + binary8);
    Last edited by ZOverLord; December 14th, 2008 at 01:08 PM.

  5. #5
    Join Date
    Dec 2008
    Posts
    5

    Re: Display an int in Binary

    thx a lot , just what i am looking for.

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

Featured