Conversion of Decimal to any Base
Số trang: 5
Loại file: doc
Dung lượng: 121.00 KB
Lượt xem: 19
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
C # chuyển đổi chức năng rất mạnh mẽ. Lớp Chuyển đổi có nhiều phương pháp để thực hiện các
điển hình chuyển đổi của một chuỗi số nguyên, và các loại dữ liệu khác. Một trong những
chức năng ToInt64 () không chỉ chuyển đổi một giá trị cho đến 64bit
số nguyên mà còn trả về
số cơ sở quy định.
Nội dung trích xuất từ tài liệu:
Conversion of Decimal to any Base Conversion of Decimal to any Base (Binary, Octal or Hexa) and viceversa (C#) By Balamurali Balaji | 27 May 2005 .NET1.0 C#Windows DevIntermediate Generic functions written in C# to convert a binary,octal or a hexadecimal to a decimal number and viceversa. See Also • Articles like this • Articles by this author 14 Article Browse Code Stats Revisions 3.03 (16 votes) Sponsored Links Download source and demo files 10.9 Kb • Introduction C# conversion functions are very powerful. The Convert class has many methods that perform typical conversion of a string to integer, and other data types. One of the functions ToInt64() not only converts a given value to a 64bit integer but also returns the number to the specified base. Collapse Convert.ToInt64(value,16); The above function converts a decimal number to hexadecimal number. This is most useful in situations where you write programs for arithmetic calculations. I thought of writing my own function that does this kind of conversion. I created two functions, DecimalToBase which converts any decimal number to a binary, octal or hexadecimal number and BaseToDecimal which converts any binary, octal or hexadecimal number to the corresponding decimal number. The most interesting part is that both the functions are generic in nature. In the DecimalToBase function, I had to write the algorithm that can perform the conversion of a decimal to binary, octal or hexadecimal. I had to write the code for all of these conversions within a single function, still keeping my code reduced to handle all sorts of values. C# ?s typesafe nature prevents us from doing some easier characterint conversions and we just can? t surpass them, as it would produce adverse results. Same is the case with the BaseToDecimal function. A sample application to test the conversion functions Create a sample Windows application in Visual Studio. NET. • In the form: • a. Add three label controls named label1, label2, and label3. b. Add three text box controls named textBox1, textBox2, and textBox3. c. Place a button control named button1 and set its Text property to ?To Base?. d. Place another button control named button2 and set its Text property to ?To Base? . Write the following declarations as data members of the form class: • Collapse const int base10 = 10; char[] cHexa = new char[]{'A','B','C','D','E','F'}; int[] iHexaNumeric = new int[] {10,11,12,13,14,15}; int[] iHexaIndices = new int[] {0,1,2,3,4,5}; const int asciiDiff = 48; Include the functions as member methods in the form class module. • This function takes two arguments; the integer value to be converted and the base value (2, 8, or 16) to which the number is converted to: Collapse string DecimalToBase(int iDec, int numbase) { string strBin = ; int[] result = new int[32]; int MaxBit = 32; for(; iDec > 0; iDec/=numbase) { int rem = iDec % numbase; result[--MaxBit] = rem; } for (int i=0;i= base10) strBin += cHexa[(int)result.GetValue(i)%base10]; else strBin += result.GetValue(i); strBin = strBin.TrimStart(new char[] {'0'}); return strBin; } This function takes two arguments; a string value representing the binary, octal, or hexadecimal value and the corresponding integer base value respective to the first argument. For instance, if you pass the first argument value 1101, then the second argument should take the value 2. Collapse int BaseToDecimal(string sBase, int numbase) { int dec = 0; int b; int iProduct=1; string sHexa = ; if (numbase > base10) for (int i=0;i=0; i--,iProduct *= numbase) { string sValue = sBase[i].ToString(); if (sValue.IndexOfAny(cHexa) >=0) b=iHexaNumeric[sHexa.IndexOf(sBase[i])]; else b= (int) sBase[i] - asciiDiff; dec += (b * iProduct); } return dec; } Write the code in the click event of the buttons as follows: • On clicking the first button, you can convert a decimal number to any base you select in the ComboBox. Collapse private void button1_Click(object sender, System.EventArgs e) { textBox3.Text = DecimalToBase(Int32.Parse(textBox1.Text), Int32.Parse(comboBox1.Text)); } On clicking the second button, you can convert the base number you have selected in the ComboBox (binary, octal, or hexadecimal) to a decimal number. Collapse private void button2_Click(object sender, System.EventArgs e) { textBox3.Text = BaseToDecimal(textBox1.Text, Int32.Parse(comboBox1.Text)).ToString(); } Save the project. Build the solution. • Output is as shown below: • Sample Output #1: Sample Output #2: Sample Output #3: Sample Output #4: License This article, along with any associated source code and files, is lice ...
Nội dung trích xuất từ tài liệu:
Conversion of Decimal to any Base Conversion of Decimal to any Base (Binary, Octal or Hexa) and viceversa (C#) By Balamurali Balaji | 27 May 2005 .NET1.0 C#Windows DevIntermediate Generic functions written in C# to convert a binary,octal or a hexadecimal to a decimal number and viceversa. See Also • Articles like this • Articles by this author 14 Article Browse Code Stats Revisions 3.03 (16 votes) Sponsored Links Download source and demo files 10.9 Kb • Introduction C# conversion functions are very powerful. The Convert class has many methods that perform typical conversion of a string to integer, and other data types. One of the functions ToInt64() not only converts a given value to a 64bit integer but also returns the number to the specified base. Collapse Convert.ToInt64(value,16); The above function converts a decimal number to hexadecimal number. This is most useful in situations where you write programs for arithmetic calculations. I thought of writing my own function that does this kind of conversion. I created two functions, DecimalToBase which converts any decimal number to a binary, octal or hexadecimal number and BaseToDecimal which converts any binary, octal or hexadecimal number to the corresponding decimal number. The most interesting part is that both the functions are generic in nature. In the DecimalToBase function, I had to write the algorithm that can perform the conversion of a decimal to binary, octal or hexadecimal. I had to write the code for all of these conversions within a single function, still keeping my code reduced to handle all sorts of values. C# ?s typesafe nature prevents us from doing some easier characterint conversions and we just can? t surpass them, as it would produce adverse results. Same is the case with the BaseToDecimal function. A sample application to test the conversion functions Create a sample Windows application in Visual Studio. NET. • In the form: • a. Add three label controls named label1, label2, and label3. b. Add three text box controls named textBox1, textBox2, and textBox3. c. Place a button control named button1 and set its Text property to ?To Base?. d. Place another button control named button2 and set its Text property to ?To Base? . Write the following declarations as data members of the form class: • Collapse const int base10 = 10; char[] cHexa = new char[]{'A','B','C','D','E','F'}; int[] iHexaNumeric = new int[] {10,11,12,13,14,15}; int[] iHexaIndices = new int[] {0,1,2,3,4,5}; const int asciiDiff = 48; Include the functions as member methods in the form class module. • This function takes two arguments; the integer value to be converted and the base value (2, 8, or 16) to which the number is converted to: Collapse string DecimalToBase(int iDec, int numbase) { string strBin = ; int[] result = new int[32]; int MaxBit = 32; for(; iDec > 0; iDec/=numbase) { int rem = iDec % numbase; result[--MaxBit] = rem; } for (int i=0;i= base10) strBin += cHexa[(int)result.GetValue(i)%base10]; else strBin += result.GetValue(i); strBin = strBin.TrimStart(new char[] {'0'}); return strBin; } This function takes two arguments; a string value representing the binary, octal, or hexadecimal value and the corresponding integer base value respective to the first argument. For instance, if you pass the first argument value 1101, then the second argument should take the value 2. Collapse int BaseToDecimal(string sBase, int numbase) { int dec = 0; int b; int iProduct=1; string sHexa = ; if (numbase > base10) for (int i=0;i=0; i--,iProduct *= numbase) { string sValue = sBase[i].ToString(); if (sValue.IndexOfAny(cHexa) >=0) b=iHexaNumeric[sHexa.IndexOf(sBase[i])]; else b= (int) sBase[i] - asciiDiff; dec += (b * iProduct); } return dec; } Write the code in the click event of the buttons as follows: • On clicking the first button, you can convert a decimal number to any base you select in the ComboBox. Collapse private void button1_Click(object sender, System.EventArgs e) { textBox3.Text = DecimalToBase(Int32.Parse(textBox1.Text), Int32.Parse(comboBox1.Text)); } On clicking the second button, you can convert the base number you have selected in the ComboBox (binary, octal, or hexadecimal) to a decimal number. Collapse private void button2_Click(object sender, System.EventArgs e) { textBox3.Text = BaseToDecimal(textBox1.Text, Int32.Parse(comboBox1.Text)).ToString(); } Save the project. Build the solution. • Output is as shown below: • Sample Output #1: Sample Output #2: Sample Output #3: Sample Output #4: License This article, along with any associated source code and files, is lice ...
Tìm kiếm theo từ khóa liên quan:
chuyển đổi chức năng chương trình lập trình lập trình căn bản thủ thuật lập trình chuyển đổi thập phânGợi ý tài liệu liên quan:
-
114 trang 242 2 0
-
80 trang 222 0 0
-
Thủ thuật giúp giải phóng dung lượng ổ cứng
4 trang 217 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
Hướng dẫn lập trình với Android part 4
5 trang 156 0 0 -
Giáo trình Lập trình C căn bản - HanoiAptech Computer Education Center
136 trang 134 0 0 -
142 trang 130 0 0
-
124 trang 113 3 0
-
150 trang 104 0 0
-
78 trang 103 0 0
-
7 trang 85 0 0
-
87 trang 80 0 0
-
8 trang 79 0 0
-
81 trang 68 0 0
-
72 trang 50 1 0
-
Ngân hàng đề thi học phần Nhập môn tin học - Nhập môn lập trình
18 trang 44 0 0 -
Ngân hàng câu hỏi trắc nghiệm về lập trình web ASP.Net (C#)
11 trang 44 0 0 -
The CISA Prep Guide Mastering the Certified Information Systems Auditor Exam phần 1
60 trang 43 0 0 -
80 trang 41 0 0
-
Những công cụ chỉnh sửa video trực tuyến
4 trang 41 0 0