Wrox Programmer Forums > C# decimal string to binary
 |
 C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

July 18th, 2004, 06:36 AM
 se7ss Registered User Join Date: Jul 2004 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
decimal string to binary

can any one help me with writing a function that converts a decimal
string just up to 255 to a binary using gets();
ex:
enter a number:-
255
255 in decimal strig = '11111111' in binary string

enter a number: 8
8 in decimal string = 00001000 in binary string

with the attention for writing the leading zeros at the first of the binary string
thanks

July 18th, 2004, 10:17 AM
 bmains Friend of Wrox Join Date: Jun 2003 Posts: 1,998 Thanks: 0 Thanked 3 Times in 3 Posts

Hello,

Each 0 or 1 has a numerical value that is computed when calculating the 1's, which the slots are:

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1

If you subtract 128 from 255 and you get a positive number, then you put a 1 in the first column; otherwise you enter a zero. Then go down the line. From the new value of 127, you subtract 64 and if you get a positive number, you put a one in the 64 column; otherwise, enter zero.

And so on and so forth.

Brian

July 18th, 2004, 04:54 PM
 mehdi62b Friend of Wrox Join Date: Jul 2004 Posts: 623 Thanks: 0 Thanked 1 Time in 1 Post

I didn't get exactly what you mean
but as we did in c++ period!!! I think you need two functions here
one for converting your char variables(obtained with get()) to integer and the second for converting an integer
to binary
_________________________________
int convertChartoInt(char ch1,char ch2,char ch3)
{
return (ch1-48)*100+(ch2-48)*10+(ch3-48);
}
void convertDecimaltoBinary(int decimal)
{
int i=8; //The number of bits
int test=128; //10000000
while(i)
{
int r=0;
if (decimal&test) r=1; //& is a logical operator for AND two operands
Console::Write(r); //write the MSB(Most Significant Bit) of your changing decimal evry time
decimal<<=1; //this shifts decimal to left one bit
i--;
}
}
-----------------------------------------------
you know in .NET you can do all of them in one line
also you could use the method Brian mentioned(sequent divisions with 2 and sequent productions by 1,10,100,... and also sequent additions for getting the the result within a decimal number)
I realy didn't get what I said!!!!:D:D
-----------------------------------
int i=1;
int result;
while(decimal)
{
r=decimal%2;
decimal/=2;
result+=r*i;
i*=10
}
--------------------------------
Then change the output forrmat for printing the leading zeros.

--------------------------------------------
Mehdi
I'm waiting for your better idea.

July 18th, 2004, 05:44 PM
 se7ss Registered User Join Date: Jul 2004 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

thanks guys ..
but there is a problem with converting my decimal (STRING) i think bmains is treating the decimal as an integer while its as a string...Oram i wrong??

and mehdi62b i have a question for u ..what if u are passing just 2 characters to the function like the number 14 or 13 qhat happens ...thanks guys i am only a newbee so bare with me thanks,,,

July 19th, 2004, 01:11 AM
 mehdi62b Friend of Wrox Join Date: Jul 2004 Posts: 623 Thanks: 0 Thanked 1 Time in 1 Post

Dear se7ss write another two functions for overloading one digit and two digits.
meanwhile,params Array(in .NET or maybe in c++)could be a good method

--------------------------------------------
Mehdi
I'm waiting for your better idea.

July 19th, 2004, 02:52 PM
 tnd Authorized User Join Date: Jun 2004 Posts: 40 Thanks: 0 Thanked 0 Times in 0 Posts

Look in MSDN in contents under
Visual Studio .NET
.NET Framework
Programming with the .NET Framework
Working with Base Types
Customizing Format Strings

The example shows how to convert an integer into binary and returns it in string format.

 Similar Threads Thread Thread Starter Forum Replies Last Post 2's Complement binary to decimal conversion gbilios C++ Programming 7 February 25th, 2013 09:46 AM Problem converting from string to decimal georgetjojo Classic ASP Basics 1 November 29th, 2007 07:00 PM Decimal to String ?!? TPP XSLT 1 January 18th, 2007 12:08 PM PROGRAM FOR DECIMAL TO BINARY CONVERSION bharathidhas C# 1 December 13th, 2006 02:53 AM Decimal to Binary Rafter VB.NET 2002/2003 Basics 1 February 9th, 2006 10:23 AM