|
Indeed your code is better for the real world. My point was about (badly) reinventing the string-to-number conversion.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
You know if I saw a piece of code that said binary to decimal, and if I said 1001, I'd expect to get 9 back instead of 1001! You would be in a world of hurt if I entered an 11 digit "binary" number and that is only slightly longer than 1/3 of a 32 bit int.
|
|
|
|
|
If you are saying what I think you are saying, then you need to take a close look at the second argument in the call to Convert.ToInt32() . It wouldn't be me that hurt.
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
True, didn't even notice you were using an overload I didn't know about.
|
|
|
|
|
Hello mehmetali,
peter have excelent solution ,
I made mine own which is not as elegant.
It is console application made in SharpDevelop C#.
This code is update of old one, it have no problem with entry of numbers as old version,
now it works with float point numbers and it folows full math logic.
using System;
namespace Binary_to_Decimal
{
class Program
{
public static void Main(string[] args)
{
string [] DecimalFloat = new string[2];
string Binary = "";
bool RealRelevance = false;
bool BinaryRelevance = false;
int Index = 0;
int Lenght = 0;
int Exponent = 0;
int Cipher = 0;
double BaseExponent = 0;
double RealNumber = 0;
double D = 0;
double F = 0;
string Negative = "";
string DecimalPart = "";
string FloatPart = "";
string Letter = "";
begining :
Console.Title = "Program Binary to Real ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Binary to Real");
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Enter binary number (exmpl: 1001) : ");
Binary = "";
Binary = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
RealRelevance = false;
RealNumber = 0;
RealRelevance = double.TryParse(Binary,out RealNumber);
if (RealRelevance == true)
{
if (Binary.Contains("-"))
{
Binary = Binary.TrimStart('-');
Negative = "- ";
RealNumber = -1 * RealNumber;
}
else if (!Binary.Contains("-") && !Binary.Contains("+"))
{
Negative = "";
}
else if (Binary.Contains("+"))
{
Binary = Binary.TrimStart('+');
Negative = "+ ";
RealNumber = double.Parse(Binary.ToString());
}
if ( Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
DecimalPart = DecimalPart.TrimStart('0');
if (DecimalPart =="")
{
DecimalPart = "0";
}
FloatPart = FloatPart.TrimEnd('0');
if (FloatPart =="")
{
FloatPart = "0";
}
if (DecimalPart =="0" && FloatPart =="0")
{
Binary = "0";
}
if (DecimalPart !="0" && FloatPart =="0")
{
Binary = DecimalPart;
}
if (DecimalPart =="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
if (DecimalPart !="0" && FloatPart !="0")
{
Binary = DecimalPart + "," + FloatPart;
}
}
else
{
Binary = Binary.TrimStart('0');
if (Binary =="")
{
Binary = "0";
}
}
}
else if (RealRelevance == false)
{
Console.WriteLine("Error , you didn't entered a number at all..");
Console.WriteLine();
goto end;
}
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for (Index=0;Index<Lenght ;Index = Index + 1)
{
Letter = Binary.Substring(Index,1);
if (Letter != "0" && Letter != "1" && Letter !=",")
{
BinaryRelevance = false;
Index = Lenght;
}
else
{
BinaryRelevance = true;
}
}
if (BinaryRelevance != true)
{
Console.WriteLine();
Console.WriteLine("Error , you didn't enter binary number ...");
Console.WriteLine();
goto end;
}
if (Binary.Contains(","))
{
DecimalFloat = Binary.Split(',');
DecimalPart = DecimalFloat[0];
FloatPart = DecimalFloat[1];
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
D = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = DecimalPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = DecimalPart.Substring(Index,1);
Cipher = int.Parse(Letter.ToString());
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
D = D + (Cipher * BaseExponent);
}
Exponent = 0;
BaseExponent = 0;
Cipher = 0;
F = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = FloatPart.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Index+1;
Letter = FloatPart.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,-Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
F = F + (Cipher * BaseExponent);
}
RealNumber = D + F;
goto write;
}
else
{
Exponent = 0;
Cipher = 0;
RealNumber = 0;
Index = 0;
Lenght = 0;
Letter = "";
Lenght = Binary.Length;
for(Index=0;Index<Lenght ;Index=Index+1)
{
Exponent = Lenght - Index - 1;
Letter = Binary.Substring(Index,1);
BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());
Cipher = int.Parse(Letter.ToString());
RealNumber = RealNumber + (Cipher * BaseExponent);
}
}
write:
if (RealNumber == 0)
{
Negative ="";
}
Console.WriteLine();
Console.WriteLine("Binary number : " + Negative + Binary);
Console.WriteLine();
Console.WriteLine("Real number : " + Negative + RealNumber.ToString());
Console.WriteLine();
end:
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Do you want to run program again y / n : ");
Letter = Console.ReadLine();
if ( Letter == "Y" || Letter == "y")
{
goto begining;
}
}
}
}
Code was updated 09/02/2012.
Whole Solution for IDE SharpDevelop you can download from the bottom of this page :
Conversion from one to another number system [ Open in new window ]
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
modified 26-Feb-12 8:50am.
|
|
|
|
|
Peter's result would put "101" out as " 101" instead of " 5" Sorry, I'm wrong about that. didn't notice or know about the overload.
I still like the following logic a little better, it only allows int results, has more error checking, and gives immediate results as you type. (Change your event delegate)
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool isvalid = true;
string txt1="", txt2 = "HATA!!!";
string sayii = textBox1.Text;
int strt = 0;
int bitOffset = 1;
byte x;
if (sayii.Length == 0)
{
txt1 = "Deger girilecek alan boş.";
isvalid = false;
}
else if (sayii.Length > 32)
{
txt1 = "A binary integer can only be 32 bits long";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(0, 32);
return;
}
else if (sayii.Length == 32)
{
x = (byte)sayii[0];
if (x < 48 || x > 49)
{
txt1 = "A binary value must be 1 or 0";
txt2 = "HATA!!!";
isvalid = false;
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = sayii.Substring(1, 31);
return;
}
if (x == 49) strt = int.MinValue;
sayii = sayii.Substring(1, 31);
}
for (int i = sayii.Length; i > 0 && isvalid; )
{
x = (byte)sayii[--i];
if (x == 49)
strt += bitOffset;
else if (x != 48)
{
txt1 = "A binary value must be 1 or 0";
isvalid = false;
}
bitOffset *= 2;
}
if (!isvalid)
{
MessageBox.Show(txt1, txt2, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else textBox2.Text = " " + strt.ToString();
}
modified 10-Oct-11 21:09pm.
|
|
|
|
|
See this reply[^] above. The same comment applies here.
Also, have a look at this thread[^].
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Peter_in_2780 wrote: Also, have a look at this thread[^].
"this thread" got me here in the first place.
I know this is inefficient, but as a learning tool, this makes a good example of how signed integers work. (and maybe even let him learn how to code a little more efficiently.) I think his original example would say "1xyz1" was 33. (I don't remember his original code exactly, so I'm going from what I remember thinking of it.)
I actually learned something. My version has a bug in it. My "bit *= 2;" (my expectation and I forgot what I used for "bit") should have blown up when I tried to make it 2^32 power, instead it made it -2^31 (int.MinValue). To fix my bug, add "if (i > 0) " before that statement and take out the prior "if ...Length == 32" logic.
|
|
|
|
|
Are there any good reporting and database tools that you would recommend for C#?
Most likely would be using Visual C# Express for the IDE.
Something similar to the RAD tool in MS Access would be nice.
Thank you.
|
|
|
|
|
Stan Moong wrote: Are there any good reporting and database tools that you would recommend for C#?
I keep recommending Microsoft Access, as it's a great reporting-tool. It's kinda easy to put the data from Sql Server into an Access database, and it's damned easy to make a report in that database. Users can view the reports using the free runtime version[^].
It's my favorite reporting tool, as I can export the data that the report is based on along with the report (ideal when debugging), it's easy in maintenance, and well integrated in Office - doing a mailmerge from there shouldn't be a problem. An Access database can also contain custom code or forms, giving you a fast and flexible reporting-engine.
Have worked with Crystal (gave us headaches on updating), DevExpress (kinda nice, if you don't mind the DevExpress way of working) and the Report Designer that's in Visual Studio (showing them using the ReportViewer[^]). Each will have it's own merits; best would be to take a day to give them a try.
Bastard Programmer from Hell
|
|
|
|
|
Thank you for the information, Eddy. Will test them out.
|
|
|
|
|
I have seen many applications that make use of the ActiveSync connection to a mobile computer to perform functions. A couple of well known examples are remote registry editors and remote display tools. I'd like to understand the architecture better to know how to invoke a method on the desktop side that acts on the mobile computer side. For example, I'd like to write an application that detects when a device is up on ActiveSync, and then displays information about the mobile computer such as serial number, MAC address, manufacturer, model, etc. Many of these are P/Invoke calls, and I can write WinForm applications that get some of these variables that run on the device locally. I would like to better understand how to use ActiveSync to invoke such calls on a desktop WPF application, get the information from the device, and display it in a table within the WPF application. Would I make use of a socket of some kind? I don't know where to start with this.
Regards,
Scott
|
|
|
|
|
Hi
I am trying to build a calculator and use a WCF web service to do the math part. I need to pass the two integer arguments to my web service.
I defined the numbers on calculator as buttons.
Now I need to pass input to web service based on the two values clicked on calculator.
Can you please tell me how do I get the values into the button1 click event based on button4 and bitton 2. ( For now I just have two numbers on calculator..)
public partial class Calculator : System.Web.UI.Page
{
private int i, j;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MathClient client = new MathClient();
try
{
int ii = client.Add(5, 6);
Result.Text = Convert.ToString(ii);
}
catch (Exception ex)
{
Result.Text = ex.Message;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int i, j;
i = 4;
j = 4;
}
protected void Button2_Click(object sender, EventArgs e)
{
int i, j = 4;
}
}
|
|
|
|
|
When you declare the integer variables i and j inside the Button Click event of Buttons 3 and 2: you create are creating variables local to those event handlers: so your class-scoped integer variables with the same names are never getting set.
The Button1 Click event will, therefore, also see i and j as having their default value: 0.
So set i and j in the Buttons 3 and 2 event handlers, but do not declare them there.
best, Bill
"Use the word 'cybernetics,' Norbert, because nobody knows what it means. This will always put you at an advantage in arguments." Claude Shannon (Information Theory scientist): letter to Norbert Weiner of M.I.T., circa 1940
|
|
|
|
|
Greetings Gurus,
I have been hunting around for a while now and would like to know if there is a way to get system information such as pc manufacturer, pc model, pc serial number, total/available memory (RAM) slots etc. etc.
using C# but without using WMI (which I find to be a bit lumpy and resource intensive)?
Eagerly awaiting your response/s...
Thank you.
|
|
|
|
|
You can find a lot without WMI, but then most of it will require access to Win32 functions, hence P/Invoke.
There is more information available inside WMI (e.g. the details of the memory slots).
A lot of information is optional, manufacturers can provide it, but they don't have to. Most of that, when available, requires WMI.
And finally getting some information requires administrative rights (and will cause UAC dialogs on Vista and above).
|
|
|
|
|
Hi Luc,
I have extracted about as much relevant information as I can from System.Environment, SystemInformation, System.Net.NetworkInformation & registry.localmachine as possible but I was hoping to pull some of the WMI-type information without using WMI.
I was wondering if there was a "System" namespace available that could fill in the blanks.
|
|
|
|
|
No, there isn't, other than WMI. You're going to have to write a TON of code to get even a tenth of the information you can get from WMI, and the code you write is not going to work on all systems and on all hardware revisions. Still want to avoid WMI??
|
|
|
|
|
Hi Dave,
If I can
I read a forum post about using System.VisualBasic.Devices's ComputerInfo but can't get it to work.
Have you come across that namespace?
|
|
|
|
|
What's so hard about it?
using Microsoft.VisualBasic.Devices;
...
Computer c = new Computer();
Debug.WriteLine("O/S: " + c.Name);
EDIT: My bad, I forgot which forum I was in...
|
|
|
|
|
I developed a small remote application, and connected with one of my client. But everytime Iam using the below code to connect with my server and accessing the sqlserver...And hence I found my application is slow at data loading and saving...
Is there any other way to connect the server faster manner ? or Any other approaches...
Thanks for the helps
public string WMSCon_1 = "Data Source=172.178.1.120; initial Catalog=Sales_Master; User Id=codeproject; Password=xxxxxxx"
Actually I given the above connection string and connecting with the client for data access. Simply every where Iam giving the ip and just connecting, whether I have to keep any other things for this remote access to increase the speed..?
Thanks
PARAMU
|
|
|
|
|
Paramu1973 wrote: remote application
What do you mean by "remote"?
Paramu1973 wrote: my application is slow at data loading and saving
You would probably have to show a sample of how you're doing it; it may be very inefficient code. It's unlikely to be due to the connection string.
Perhaps you'll want a Web Service.
|
|
|
|
|
Hi,
Thanks for the helps.
There is nothing to say inefficient. Since I just connect via IP as mentioned in my prior post. Yes. As per the advice, I will try to write the web service and may be the best solution.
|
|
|
|
|
Hi everyone, with the below code, I'm able to draw a rectangle by clicking and dragging the mouse. So the question is: Can I access the controls within that selected rectangle/area? Any clue or a piece of code that puts me in the right direction would be appreciated. Thanks
Rectangle re;
private void form_MouseDown(object sender, MouseEventArgs e)
{
re.X = e.X;
re.Y = e.Y;
re.Width = 0;
re.Height = 0;
}
private void form_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
re.Width = e.X - re.X;
re.Height = e.Y - re.Y;
this.Invalidate();
}
}
private void form_Paint(object sender, PaintEventArgs e)
{
SolidBrush myBrush;
myBrush = new SolidBrush(Color.PaleTurquoise);
e.Graphics.FillRectangle(myBrush, re);
}
private void Form1_Click(object sender, EventArgs e)
{
re = new Rectangle(0, 0, 0, 0);
this.Invalidate();
}
|
|
|
|
|
Without trying it, off the top of my head, one way to achieve this would be to use the Rectangle.IntersectsWith method. Basically, each control on your form has a corresponding rectangle associated with it, and you would be able to compare this with the rectangle you've dragged out.
Note that this assumes you want to pick any control touched by your rectangle. If the child controls must be inside the rectangle, you could use Rectangle.Contains . As an optimisation, I'd be tempted to do one pass and add the control id and rectangle into a Dictionary so that you don't have to look it up every time.
|
|
|
|
|