there is no inbuilt function in .net there may be some c# function freely availabe in some blog, please try searching in google and bing with different keywords
Unfortunately, AFAIK, unlike languages that preceding c#,
octal and binary can not be easily represented.
edit: assembler has binary ... but i can not recall binary in c or c++ ...
because i've worked in many languages, i sometimes
mix my facts ... for c#, it's best to always go to the c# reference: http://msdn.microsoft.com/en-us/library/618ayhy6
You could write your own function relatively easily.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21
Hi Paul
Doesn't the code shown in my original post do what you've done in a simpler and succinct way?:
rssadiq
Convert the inputs (binary numbers) into Decimal using
Convert.ToInt64("101", 2)
I think, C# doesn't provide a way to specify binary nubmers as Mudasir.Khan mentioned earlier. So, I'm going to take his post as an answer to the question. But thank you ALL for your time and nice suggestions.
sorry, but your code does not compile in LINQPad 4:
string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21
LINQPad 4 errors:
; expected
Invalid expression term '=>'
Invalid expression term ')'
ditto vs2010:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace forums_asp_net_post_5057470_aspx_Paul_Linton
{
class Program
{
static void Main(string[] args)
{
string input = "10101";
int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0));
// y == 21
}
}
}
vs 2010 error messages:
Error 1 Invalid expression term '=>' ...\Program.cs Line 13 column 64
Error 2 ; expected ...\Program.cs Line 13 column 64
Error 3 ; expected ...\Program.cs Line 13 column 67
Error 4 Invalid expression term ')' ...\Program.cs Line 13 column 87
Error 5 ; expected ...\Program.cs Line 13 column 87
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
rssadiq
Member
2 Points
6 Posts
How can we tell the compiler that the input numbers are in Binary Format.
Jul 08, 2012 04:46 PM|LINK
Hi
I want to add two binary numbers and show the result in Binary. For example, 101 +101 =1010
I can do this in the following steps:
Convert.ToInt64("101", 2)Is there any way of adding them directly without converting them into intergers?
In fact, I want to know how we can tell the compiler that the input numbers are in Binary Format?
Regards
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 08, 2012 05:01 PM|LINK
Assalamualikum,
there is no inbuilt function in .net there may be some c# function freely availabe in some blog, please try searching in google and bing with different keywords
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 08, 2012 07:31 PM|LINK
This will not help you that much, but you could use hexadecimal:
output
Unfortunately, AFAIK, unlike languages that preceding c#,
octal and binary can not be easily represented.
edit: assembler has binary ... but i can not recall binary in c or c++ ...
because i've worked in many languages, i sometimes
mix my facts ... for c#, it's best to always go to the c# reference:
http://msdn.microsoft.com/en-us/library/618ayhy6
You could write your own function relatively easily.
g.
rssadiq
Member
2 Points
6 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 08, 2012 08:35 PM|LINK
Assalamulaikum Mudasir
I wanted a way to specify the binary numbers the way we can specify hexadecimal [in c# as gerry has shown]
or octal and hexadecimal [in C/C++].
Paul Linton
Star
13407 Points
2533 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 08, 2012 10:25 PM|LINK
Error checking is left as an exercise for the reader.
rssadiq
Member
2 Points
6 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 09, 2012 01:36 PM|LINK
Hi Paul
Doesn't the code shown in my original post do what you've done in a simpler and succinct way?:
I think, C# doesn't provide a way to specify binary nubmers as Mudasir.Khan mentioned earlier. So, I'm going to take his post as an answer to the question. But thank you ALL for your time and nice suggestions.
Paul Linton
Star
13407 Points
2533 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 09, 2012 09:32 PM|LINK
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 10, 2012 02:54 AM|LINK
sorry, but your code does not compile in LINQPad 4:
LINQPad 4 errors:
ditto vs2010:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace forums_asp_net_post_5057470_aspx_Paul_Linton { class Program { static void Main(string[] args) { string input = "10101"; int y = input.ToCharArray().Aggregate(0, total, x) => total*2+(x=='1'?1:0)); // y == 21 } } }vs 2010 error messages:
g.
Paul Linton
Star
13407 Points
2533 Posts
Re: How can we tell the compiler that the input numbers are in Binary Format.
Jul 10, 2012 03:44 AM|LINK
Missing one left paren. See if you can work out where it goes.