I want to convert the decimal number to the binary number and save it to the number group. Please check if I wrote it correctly. Thank you.

Source: Internet
Author: User
I want to convert the decimal number to the binary number and save it to the number group. Please check if I wrote it correctly. Thank you. Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061206215537250.html
Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, mask;

Type
Tform1 = Class (tform)
Maskedit1: tmaskedit; // enter a number from here
Label1: tlabel;
Label2: tlabel;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
X, I: integer; // can I declare a variable here? Should it be declared in the button event?
Array16: array [1 .. 16] of integer; // save it in this array
{Public declarations}
End;

VaR
Form1: tform1;
Implementation

{$ R *. DFM}

Procedure tform1.button1click (Sender: tobject );
Begin
X: = strtoint (form1.maksedit1. Text); // assign a value to the intermediate variable X
I: = 16;
Repeat
Array16 [I]: = x mod 2; // even the original method is used here, and the remainder is obtained continuously.
X: = x Div 2;
I: = I-1;
Until I = 0;
End;
End.

------------------------------------ Split line --------------------------------
Program Can run
1. I will not check whether the result is correct. I will only assign one of the arrays to another edit. text to see if a single digit is correct. Is there any way to check whether the entire array is correct?
2. the attribute of maskedit1 is 9999; 0;
When the input is 4 bits, you can click the button successfully, but if the number is less than 4 bits, an error occurs, indicating that the value is not valid.
3. Is there any kind of inttobinary () function? If so, how can I implement my requirements? I have seen some examples where continuous SHR is transferred to a string and how can I implement it in an array?
4. How can I define this process if I want to use it elsewhere? Where is the definition written, and where is the subject written? What is the format of the call?
------------------------------------ Split line ---------------------------------
I am a newbie. I hope my friends can read it patiently and help solve the problem. Please try to give me more details. Thank you!

Add
2. the attribute of maskedit1 is! 9999; 0; 3rd attributes with spaces and 0 have been tried and the problem persists.

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, mask;

Type
Tform1 = Class (tform)
Maskedit1: tmaskedit; // enter a number from here
Label1: tlabel;
Label2: tlabel;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
X, I: integer; // can I declare a variable here? Should it be declared in the button event?
Variables can be declared here and in the button, but the life cycle is different.
Array16: array [1 .. 16] of integer; // save it in this array
There seems to be no need to use integer here, which is a waste.
{Public declarations}
End;

VaR
Form1: tform1;
Implementation

{$ R *. DFM}

Procedure tform1.button1click (Sender: tobject );
Begin
X: = strtoint (form1.maksedit1. Text); // assign a value to the intermediate variable X
I: = 16;
Repeat
Array16 [I]: = x mod 2; // even the original method is used here, and the remainder is obtained continuously.
X: = x Div 2;
I: = I-1;
Until I = 0;
End;
End.

I suggest you look for this object Pascal or Delphi book for your many questions later,


I wrote an example for you: converting an integer to a binary string for output.

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Edit1: tedit;
Button2: tbutton;
Edit2: tedit;
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

Function inttobinarystr (theval: longint): string;
VaR
Counter: longint;
Begin
If theval = 0 then begin
Result: = '0 ';
Exit;
End;
Result: = '';
Counter: = $80000000;
{Suppress leading zeros}
While (counter and theval) = 0) Do begin
Counter: = counter SHR 1;
If (counter = 0) Then break; {we found our first "1 ".}
End;
While counter> 0 do begin
If (counter and theval) = 0 then result: = Result + '0'
Else result: = Result + '1 ';
Counter: = counter SHR 1;
End;
End;


Procedure tform1.button2click (Sender: tobject );
Begin
Edit2.text: = inttobinarystr (strtoint (edit1.text ))
End;

End.


Grace hasn't learned too well. I borrowed some books to explain Pascal in less space.

I will create a serial transmission control program later.

Upload data by one character

Upload each bit of the array to the serial port one after another

If you do not use controls and do not use APIs, the Assembly is embedded in 98 systems.

If it is string type, how to implement one-bit

Continue
Thank you.

I do not know whether a "digit" is a character in string. If yes, then:

For example:
String: = 'abcdefg ';
Then:
String [1] = 'a ';
String [2] = 'B ';
...

We strongly despise people who will not close the post after the problem is solved!
People who care about technical issues and transfer their posts to non-technical areas will be despised!
Despise you!

Http://community.csdn.net/Expert/topic/5216/5216675.xml? Temp =. 9262659.

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.