Click here to Skip to main content
16,006,341 members
Home / Discussions / C#
   

C#

 
GeneralRe: c# binary to decimal code -new- Pin
mehmetali_066-Oct-11 20:47
mehmetali_066-Oct-11 20:47 
GeneralRe: c# binary to decimal code -new- Pin
Perić Željko7-Oct-11 7:47
professionalPerić Željko7-Oct-11 7:47 
GeneralRe: c# binary to decimal code -new- Pin
Peter_in_27807-Oct-11 11:49
professionalPeter_in_27807-Oct-11 11:49 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 10:57
KP Lee10-Oct-11 10:57 
GeneralRe: c# binary to decimal code -new- Pin
Peter_in_278010-Oct-11 14:42
professionalPeter_in_278010-Oct-11 14:42 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 15:05
KP Lee10-Oct-11 15:05 
NewsRe: c# binary to decimal code -new- Pin
Perić Željko7-Oct-11 7:56
professionalPerić Željko7-Oct-11 7:56 
AnswerRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 14:51
KP Lee10-Oct-11 14:51 
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)
C#
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;// previous command spawns a text changed event
    }
    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;// previous command spawns a text changed event
        }
        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.

GeneralRe: c# binary to decimal code -new- Pin
Peter_in_278011-Oct-11 12:41
professionalPeter_in_278011-Oct-11 12:41 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee11-Oct-11 13:55
KP Lee11-Oct-11 13:55 
QuestionGood reporting and database tools for C# ? Pin
Stan Moong5-Oct-11 18:22
Stan Moong5-Oct-11 18:22 
AnswerRe: Good reporting and database tools for C# ? Pin
Eddy Vluggen6-Oct-11 6:55
professionalEddy Vluggen6-Oct-11 6:55 
GeneralRe: Good reporting and database tools for C# ? Pin
Stan Moong6-Oct-11 14:31
Stan Moong6-Oct-11 14:31 
QuestionCall Methods Across ActiveSync Connection Pin
namelkcip5-Oct-11 17:02
namelkcip5-Oct-11 17:02 
QuestionBasic c# question Pin
SFORavi5-Oct-11 13:22
SFORavi5-Oct-11 13:22 
AnswerRe: Basic c# question Pin
BillWoodruff5-Oct-11 14:08
professionalBillWoodruff5-Oct-11 14:08 
QuestionGetting system specs without WMI Pin
CCodeNewbie5-Oct-11 9:30
CCodeNewbie5-Oct-11 9:30 
AnswerRe: Getting system specs without WMI Pin
Luc Pattyn5-Oct-11 10:26
sitebuilderLuc Pattyn5-Oct-11 10:26 
GeneralRe: Getting system specs without WMI Pin
CCodeNewbie5-Oct-11 11:03
CCodeNewbie5-Oct-11 11:03 
GeneralRe: Getting system specs without WMI Pin
Dave Kreskowiak5-Oct-11 15:44
mveDave Kreskowiak5-Oct-11 15:44 
GeneralRe: Getting system specs without WMI Pin
CCodeNewbie5-Oct-11 19:25
CCodeNewbie5-Oct-11 19:25 
GeneralRe: Getting system specs without WMI Pin
Dave Kreskowiak6-Oct-11 3:39
mveDave Kreskowiak6-Oct-11 3:39 
QuestionFor Remote Connection -SQL Server Pin
Paramu19735-Oct-11 4:21
Paramu19735-Oct-11 4:21 
AnswerRe: For Remote Connection -SQL Server Pin
PIEBALDconsult5-Oct-11 5:22
mvePIEBALDconsult5-Oct-11 5:22 
GeneralRe: For Remote Connection -SQL Server Pin
Paramu19735-Oct-11 19:30
Paramu19735-Oct-11 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.