A pen: C # convert decimal to hexadecimal

Source: Internet
Author: User

As an electronic major student, I found that more and more students are turning towards software development, although our major and software are not very good. The following is a pen question that my classmates sent me. Let me finish it before Monday. The following is the details. Let's take a look.

Answer questions

Select a familiar language. Use this language to compile the following functions and use your own algorithms. Do not use functions such as printf, sprintf, format, tostring, ITOA, tohex, and so on.

Function Name

Tq_gethexstringex

Parameters

Integer inumber
Integer icount

Return Value

String type

Function Description

Obtain the inumber hexadecimal string. If the width is less than icount, add 0 to the front.

Example

Tq_gethexstringex (); "0C" is returned"
Tq_gethexstringex (); "000000c" is returned"

The principle of implementation is clear, but there is no rule in details. The principle is: enter a 10-digit number and a single-digit limit. If the number of digits after conversion is insufficient, fill in 0.

Interface Diagram


Code

Public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs E) {}/// <summary> /// obtain the initial value, merge operations /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void button#click (Object sender, eventargs e) {int inumber = int. parse (txt10.text); int icount = int. parse (txtnum. text); string n = tq_gethexstringex (in Umber, icount); txt16.text = n ;}/// <summary> /// based on the input decimal number and the specified number of digits, returns a hexadecimal string /// </Summary> /// <Param name = "inumber"> decimal number </param> /// <Param name = "icount"> specified number of digits </param> /// <returns> converted string </returns> Public String tq_gethexstringex (INT inumber, int icount) {try {string strreturn = ""; // If (inumber <16) when the number is less than 16 {// if the number of numeric characters is greater than icount, if (inumber. tostring (). length> = icount) {strreturn = inumb Er. tostring ();} // when the number is less than sixteen and the number of characters is less than icount, add 0 else {// obtain the number of 0 to be supplemented int intdif = icount-inumber. tostring (). length; strreturn = fillstring (inumber. tostring (), intdif) ;}// when the number is greater than 16, else {string strtemp = Convery (inumber ); // If (strtemp. length> icount) {strreturn = strtemp;} // when the number of converted characters is smaller than the given number, add 0 else {int intdif = icount-(Convery (inumber )). length; strreturn = fillstring (Str Temp, intdif) ;}} return strreturn;} catch (exception e) {Throw E;} finally {// todo: if this involves discarded content, delete them }}/// <summary> /// returns the string to be supplemented and the number of strings to be supplemented by 0, supplement string /// </Summary> /// <Param name = "strpre"> original string </param> /// <Param name = "intneed"> 0 </param> /// <returns> supplemented string </returns> Public String fillstring (string strpre, int intneed) {try {string strtemp = ""; string strfinal = ""; // Add 0 (Int I = 0; I <intneed; I ++) {strtemp = strtemp + "0";} strfinal = strtemp + strpre. tostring (); Return strfinal;} catch (exception e) {Throw e ;}} /// <summary> /// return the hexadecimal character corresponding to the number /// </Summary> /// <Param name = "strnum"> 10 hexadecimal digit </param> // The A-F character corresponding to <returns> </returns> Public String toletter (INT intnum) {try {string strtemp = ""; // if the value is less than 10, no conversion is required. If (intnum <10) {strtemp = intnum is returned directly. tostring (); Return strtemp;} // switch (intnum) {case 10: strtemp = "A"; break; Case 11: strtemp = "B "; break; Case 12: strtemp = "C"; break; Case 13: strtemp = "D"; break; Case 14: strtemp = "e"; break; Case 15: strtemp = "F"; break;} return strtemp;} catch (exception e) {Throw E ;}} /// <summary> /// enter a 10-digit number greater than 16, convert to hexadecimal /// </Summary> /// <Param name = "intnum"> 10 hexadecimal number </param> /// <returns> </Return S> Public String Convery (INT intnum) {try {string x = ""; int C; // used to store the number int S = 0 after dividing by 16; // array capacity int n = intnum; // used to determine the array capacity // obtain the array capacity while (n> = 16) {s ++; n = N/16 ;} // create an array string [] M = new string [S + 1]; // fill in the array int I = 0; do {c = intnum/16; // determine whether the value is greater than 10. If the value is greater than 10, convert it to ~ F format: M [I ++] = toletter (intnum % 16); intnum = C;} while (C> = 16); X = toletter (intnum ); // concatenate a string for (Int J = m. length-1; j> = 0; j --) {x + = m [J];} return X;} catch (exception e) {Throw e ;}}}

Running result


Obviously, it is not difficult to implement it. Even if there are some small problems, there are many online solutions, ...... I tried to describe the code in as much detail as possible. I also reminded my colleagues to understand the code more and hope not to learn programming just to find a job.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.