-
December 13th, 2008, 02:21 PM
#1
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?
-
December 13th, 2008, 03:22 PM
#2
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
-
December 13th, 2008, 06:16 PM
#3
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
-
December 14th, 2008, 12:11 PM
#4
Re: Display an int in Binary
Originally Posted by siulung
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.
-
December 16th, 2008, 05:40 PM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|