[WinForm&DelphiXE2] 정수값을 16진수로 변환 (ExConvertStrToIntToHex) .NET

프로그램 기능
1. 정수값을 입력하고 버튼을 누르면 16진수 값을 출력
2. 정수값이 공백이면 출력하지 않는다.
3. 정수값이 아니면 에러 메시지를 띄운다.


WinForm


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace ExConvertStrToIntToHex1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConvertToHex_Click(object sender, EventArgs e)
        {
            if (txtbxInt.Text.Trim() == "")
            {
                return;
            }

            try
            {
                Int64 value = Convert.ToInt64(txtbxInt.Text);

                txtbxHex.Text = String.Format("{0:X}", value);
            }
            catch (FormatException ex)
            {
                Debug.WriteLine("FormatException Message: " + ex.Message);

                MessageBox.Show("FormatException Message: " + ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception Message: " + ex.Message);

                MessageBox.Show("Exception Message: " + ex.Message);
            }

        }
    }
}

Delphi XE2
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    edtInt: TEdit;
    btnConvertToHex: TButton;
    edtHex: TEdit;
    procedure btnConvertToHexClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConvertToHexClick(Sender: TObject);
var
  Value: Int64;
begin
  if Trim(edtInt.Text) = '' then
  begin
    Exit;
  end;

  try
    Value := StrToInt64(edtInt.Text);

    edtHex.Text := IntToHex(Value, 1);
  except
    on e: EConvertError do
    begin
      OutputDebugString('EConvertError Message: ' + e.Message);

      ShowMessage('EConvertError Message: ' + e.Message);
    end;
    on e: Exception do
    begin
      OutputDebugString('Exception Message: ' + e.Message);

      ShowMessage('Exception Message: ' + e.Message);
    end;
  end;
end;

end.

Ref.



덧글

댓글 입력 영역