C++/CLI 进制转换器 源代码

     直到2008年我才知道,.NET上的C++已经不是托管C++而是C++/CLI(VS2005引入)。

     这是一个进制转换器的源代码,可以进行二进制,八进制,十进制,十六进制的转换。本身没什么难的地方,主要是.net framework的使用。限于整型数(_int64)的限制,输入的数太大会溢出(十六进制只能显示15位)。如果可以直接对字符串进行处理,或是对数字进行分割应该就可以实现无限大数字的进制转换了,不过我不会(汗~~)。

    值得一提的是,.net程序很“可怕”,才几十K的程序占用内存竟然十几M。不过如果你最小化程序,占用内存就会迅速下降。这是因为系统调用了SetProcessWorkingSetSize函数,通过这个函数可以实现实际内存和虚拟内存的转换。所以我在构造函数和清除按钮的响应事件中加入了        

SetProcessWorkingSetSize(::GetCurrentProcess(), -1, -1);

这并不是真正的内存优化,只是让程序在进程管理器中显的“好看”一些。   

  1. #pragma once
  2. #include   <windows.h>  
  3.                         //加入windows头文件,因为我们会用到
  4.                         //SetProcessWorkingSetSize函数
  5. namespace converter {
  6.     using namespace System;
  7.     using namespace System::ComponentModel;
  8.     using namespace System::Collections;
  9.     using namespace System::Windows::Forms;
  10.     using namespace System::Data;
  11.     using namespace System::Drawing;
  12.     /// <summary>
  13.     /// Form1 摘要
  14.     ///
  15.     /// 警告: 如果更改此类的名称,则需要更改
  16.     ///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
  17.     ///          “资源文件名”属性。否则,
  18.     ///          设计器将不能与此窗体的关联
  19.     ///          本地化资源正确交互。
  20.     /// </summary>
  21.     public ref class Form1 : public System::Windows::Forms::Form
  22.     {
  23.     public:
  24.         Form1(void)
  25.         {
  26.         InitializeComponent();
  27.         SetProcessWorkingSetSize(::GetCurrentProcess(), -1, -1);
  28.         }
  29.     protected:
  30.         /// <summary>
  31.         /// 清理所有正在使用的资源。
  32.         /// </summary>
  33.         ~Form1()
  34.         {
  35.             if (components)
  36.             {
  37.                 delete components;
  38.             }
  39.         }
  40.     private: System::Windows::Forms::Label^  label1;
  41.     protected
  42.     private: System::Windows::Forms::Label^  label2;
  43.     private: System::Windows::Forms::Label^  label3;
  44.     private: System::Windows::Forms::Label^  label4;
  45.     private: System::Windows::Forms::TextBox^  tex2;
  46.     private: System::Windows::Forms::TextBox^  tex8;
  47.     private: System::Windows::Forms::TextBox^  tex10;
  48.     private: System::Windows::Forms::TextBox^  tex16;
  49.     private: System::Windows::Forms::Button^  btnClear;
  50. private:
  51.         /// <summary>
  52.         /// 必需的设计器变量。
  53.         /// </summary>
  54.         System::ComponentModel::Container ^components;
  55. #pragma region Windows Form Designer generated code
  56.         /// <summary>
  57.         /// 设计器支持所需的方法 - 不要
  58.         /// 使用代码编辑器修改此方法的内容。
  59.         /// </summary>
  60.         void InitializeComponent(void)
  61.         {
  62.             System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
  63.             this->label1 = (gcnew System::Windows::Forms::Label());
  64.             this->label2 = (gcnew System::Windows::Forms::Label());
  65.             this->label3 = (gcnew System::Windows::Forms::Label());
  66.             this->label4 = (gcnew System::Windows::Forms::Label());
  67.             this->tex2 = (gcnew System::Windows::Forms::TextBox());
  68.             this->tex8 = (gcnew System::Windows::Forms::TextBox());
  69.             this->tex10 = (gcnew System::Windows::Forms::TextBox());
  70.             this->tex16 = (gcnew System::Windows::Forms::TextBox());
  71.             this->btnClear = (gcnew System::Windows::Forms::Button());
  72.             this->SuspendLayout();
  73.             // 
  74.             // label1
  75.             // 
  76.             this->label1->AutoSize = true;
  77.             this->label1->BackColor = System::Drawing::Color::Transparent;
  78.             this->label1->Location = System::Drawing::Point(12, 9);
  79.             this->label1->Name = L"label1";
  80.             this->label1->Size = System::Drawing::Size(53, 12);
  81.             this->label1->TabIndex = 0;
  82.             this->label1->Text = L"二进制:";
  83.             // 
  84.             // label2
  85.             // 
  86.             this->label2->AutoSize = true;
  87.             this->label2->BackColor = System::Drawing::Color::Transparent;
  88.             this->label2->Location = System::Drawing::Point(12, 38);
  89.             this->label2->Name = L"label2";
  90.             this->label2->Size = System::Drawing::Size(53, 12);
  91.             this->label2->TabIndex = 1;
  92.             this->label2->Text = L"八进制:";
  93.             // 
  94.             // label3
  95.             // 
  96.             this->label3->AutoSize = true;
  97.             this->label3->BackColor = System::Drawing::Color::Transparent;
  98.             this->label3->Location = System::Drawing::Point(12, 67);
  99.             this->label3->Name = L"label3";
  100.             this->label3->Size = System::Drawing::Size(53, 12);
  101.             this->label3->TabIndex = 2;
  102.             this->label3->Text = L"十进制:";
  103.             // 
  104.             // label4
  105.             // 
  106.             this->label4->AutoSize = true;
  107.             this->label4->BackColor = System::Drawing::Color::Transparent;
  108.             this->label4->Location = System::Drawing::Point(12, 96);
  109.             this->label4->Name = L"label4";
  110.             this->label4->Size = System::Drawing::Size(65, 12);
  111.             this->label4->TabIndex = 3;
  112.             this->label4->Text = L"十六进制:";
  113.             // 
  114.             // tex2
  115.             // 
  116.             this->tex2->Location = System::Drawing::Point(79, 6);
  117.             this->tex2->Name = L"tex2";
  118.             this->tex2->Size = System::Drawing::Size(248, 21);
  119.             this->tex2->TabIndex = 4;
  120.             this->tex2->TextChanged += gcnew System::EventHandler(this, &Form1::tex2_TextChanged);
  121.             this->tex2->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::tex2_KeyDown);
  122.             this->tex2->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::tex2_KeyPress);
  123.             // 
  124.             // tex8
  125.             // 
  126.             this->tex8->Location = System::Drawing::Point(79, 36);
  127.             this->tex8->Name = L"tex8";
  128.             this->tex8->Size = System::Drawing::Size(248, 21);
  129.             this->tex8->TabIndex = 5;
  130.             this->tex8->TextChanged += gcnew System::EventHandler(this, &Form1::tex8_TextChanged);
  131.             this->tex8->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::tex8_KeyDown);
  132.             this->tex8->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::tex8_KeyPress);
  133.             // 
  134.             // tex10
  135.             // 
  136.             this->tex10->Location = System::Drawing::Point(79, 66);
  137.             this->tex10->Name = L"tex10";
  138.             this->tex10->Size = System::Drawing::Size(248, 21);
  139.             this->tex10->TabIndex = 6;
  140.             this->tex10->TextChanged += gcnew System::EventHandler(this, &Form1::tex10_TextChanged);
  141.             this->tex10->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::tex10_KeyDown);
  142.             this->tex10->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::tex10_KeyPress);
  143.             // 
  144.             // tex16
  145.             // 
  146.             this->tex16->Location = System::Drawing::Point(79, 96);
  147.             this->tex16->Name = L"tex16";
  148.             this->tex16->Size = System::Drawing::Size(248, 21);
  149.             this->tex16->TabIndex = 7;
  150.             this->tex16->TextChanged += gcnew System::EventHandler(this, &Form1::tex16_TextChanged);
  151.             this->tex16->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::tex16_KeyDown);
  152.             this->tex16->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::tex16_KeyPress);
  153.             // 
  154.             // btnClear
  155.             // 
  156.             this->btnClear->DialogResult = System::Windows::Forms::DialogResult::Cancel;
  157.             this->btnClear->Location = System::Drawing::Point(340, 94);
  158.             this->btnClear->Name = L"btnClear";
  159.             this->btnClear->Size = System::Drawing::Size(50, 23);
  160.             this->btnClear->TabIndex = 8;
  161.             this->btnClear->Text = L"清除";
  162.             this->btnClear->UseVisualStyleBackColor = true;
  163.             this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click);
  164.             // 
  165.             // Form1
  166.             // 
  167.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
  168.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  169.             this->BackColor = System::Drawing::SystemColors::Window;
  170.             this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
  171.             this->CancelButton = this->btnClear;
  172.             this->ClientSize = System::Drawing::Size(402, 133);
  173.             this->Controls->Add(this->btnClear);
  174.             this->Controls->Add(this->tex16);
  175.             this->Controls->Add(this->tex10);
  176.             this->Controls->Add(this->tex8);
  177.             this->Controls->Add(this->tex2);
  178.             this->Controls->Add(this->label4);
  179.             this->Controls->Add(this->label3);
  180.             this->Controls->Add(this->label2);
  181.             this->Controls->Add(this->label1);
  182.             this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
  183.             this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
  184.             this->MaximizeBox = false;
  185.             this->Name = L"Form1";
  186.             this->Opacity = 0.8;
  187.             this->Text = L"进制转换器";
  188.             this->TopMost = true;
  189.             this->ResumeLayout(false);
  190.             this->PerformLayout();
  191.         }
  192. #pragma endregion
  193. //textbox改变时调用ConvertString来更新其他textbox
  194. //我们要单独对空字符串进行处理,因为函数不支持空字符串
  195. private: System::Void tex2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  196.              if (tex2->Text !="")
  197.              {
  198.              tex8->Text  = ConvertString(tex2->Text,2,8);
  199.              tex10->Text = ConvertString(tex2->Text,2,10);
  200.              tex16->Text = ConvertString(tex2->Text,2,16);
  201.              }
  202.              else
  203.              {
  204.               tex8->Text = "";
  205.               tex10->Text = "";
  206.               tex16->Text = "";
  207.              }
  208.              
  209.              }
  210. private: System::Void tex8_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  211.             if (tex8->Text !="")
  212.             {
  213.              tex2->Text  = ConvertString(tex8->Text,8,2);
  214.              tex10->Text = ConvertString(tex8->Text,8,10);
  215.              tex16->Text = ConvertString(tex8->Text,8,16);
  216.             }
  217.             else
  218.             {
  219.              tex8->Text = "";
  220.              tex10->Text = "";
  221.              tex16->Text = "";
  222.             }
  223.          }
  224. private: System::Void tex10_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  225.                if (tex10->Text !="")
  226.             {
  227.             tex2->Text  = ConvertString(tex10->Text,10,2);
  228.             tex8->Text  = ConvertString(tex10->Text,10,8);
  229.             tex16->Text = ConvertString(tex10->Text,10,16);
  230.                             }
  231.                else
  232.             {
  233.               tex2->Text = "";
  234.               tex10->Text = "";
  235.               tex16->Text = "";
  236.              }
  237.         }
  238. private: System::Void tex16_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  239.             if (tex16->Text !="")
  240.             {
  241.              tex2->Text  = ConvertString(tex16->Text,16,2);
  242.              tex8->Text  = ConvertString(tex16->Text,16,8);
  243.              tex10->Text = ConvertString(tex16->Text,16,10);
  244.                             }
  245.             else
  246.             {
  247.              tex2->Text = "";
  248.              tex8->Text = "";
  249.              tex10->Text = "";
  250.             }
  251.          }
  252. //处理按键,忽略非法按键,如二进制编辑框中输入字母
  253. private: System::Void tex2_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
  254.              wchar_t key = e->KeyChar;
  255.              if(key=='0' || key=='1' )
  256.                  e->Handled = false;
  257.              else 
  258.                  e->Handled = true;
  259.          }
  260. private: System::Void tex8_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
  261.              wchar_t key = e->KeyChar;
  262.              if (key>='0'&& key <='7'||key=='/b' ||key == (wchar_t)Keys::Delete)
  263.                  e->Handled = false;
  264.              else
  265.                  e->Handled = true;
  266.             
  267.          }
  268. private: System::Void tex10_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
  269.              wchar_t key = e->KeyChar;          
  270.              if (key>='0'&& key<='9' ||key=='/b' ||key == (wchar_t)Keys::Delete)
  271.                  e->Handled = false;
  272.              else
  273.                  e->Handled = true;
  274.          }
  275. private: System::Void tex16_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
  276.                  wchar_t key = e->KeyChar;
  277.                  if (key>='0'&&key<='9' || key>='a'&&key<='f' || key>='A'&&key<='F'||
  278.                      key=='/b' ||key == (wchar_t)Keys::Delete  )
  279.                  e->Handled = false;
  280.              else
  281.                  e->Handled = true;
  282.          }
  283. private: System::Void btnClear_Click(System::Object^  sender, System::EventArgs^  e) {
  284.              tex2->Text="";
  285.              tex8->Text="";
  286.              tex10->Text="";
  287.              tex16->Text="";
  288.              SetProcessWorkingSetSize(::GetCurrentProcess(), -1, -1);
  289.          }
  290. //字符串进制转换的程序,主要用到Convert::ToInt64和Convert::ToString
  291. //其中的异常处理主要是输入的数字太多造成数据溢出,或是复制粘贴时贴入非法字符
  292.      String^ ConvertString( String^ const% num,int fromBase,int toBase)
  293.          {
  294.              try{
  295.                  _int64 number = Convert::ToInt64(num,fromBase);
  296. //异常处理是个大麻烦
  297. //一开始直接用ToInt64内部的发出的溢出异常,但是当在十六进制
  298. //文本框中输入16个F时,数据已经溢出,十进制窗口也显示负数,但是没有异常发送
  299. //只有当再输入一个数字时才会发送溢出异常
  300. //溢出异常同时会伴随两个参数错误的异常,为什么是两个?
  301. //现在程序也有个异常问题,就是当粘贴一个负数时,不会发送格式错误或溢出异常
  302. //但是仍有异常发生,提示字符串不能包含‘-’
  303.                  if (number<0)
  304.                      throw gcnew OverflowException();
  305.                  return Convert::ToString(number,toBase);
  306.              }
  307.              catch(FormatException^)//格式错误异常
  308.              {
  309.                  MessageBox::Show("格式错啦!","错误");
  310.                  tex2->Text="";
  311.                  tex8->Text="";
  312.                  tex10->Text="";
  313.                  tex16->Text="";
  314.              }
  315.              catch(ArgumentOutOfRangeException^)//数据溢出时会有
  316.                                                              //两个参数错误异常
  317.              {
  318.              }
  319.              catch(OverflowException^ )//数据溢出异常
  320.              {
  321.                  MessageBox::Show("数据溢出啦!","错误");
  322.                  tex2->Text="";
  323.                  tex8->Text="";
  324.                  tex10->Text="";
  325.                  tex16->Text="";
  326.              }
  327.          }
  328. //组合键实现复制、粘贴、剪切、全选功能
  329. private: System::Void tex16_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
  330.             
  331.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::A)
  332.                  tex16->SelectAll();
  333.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::C)
  334.                 tex16->Copy();
  335.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::V)
  336.                  tex16->Paste();
  337.          }
  338. private: System::Void tex10_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
  339.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::A)
  340.                  tex10->SelectAll();
  341.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::C)
  342.                  tex10->Copy();
  343.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::V)
  344.                  tex10->Paste();
  345.          }
  346. private: System::Void tex8_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
  347.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::A)
  348.                  tex8->SelectAll();
  349.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::C)
  350.                  tex8->Copy();
  351.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::V)
  352.                  tex8->Paste();
  353.          }
  354. private: System::Void tex2_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
  355.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::A)
  356.                  tex2->SelectAll();
  357.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::C)
  358.                  tex2->Copy();
  359.              if(Control::ModifierKeys == Keys::Control&&e->KeyCode == Keys::V)
  360.                  tex2->Paste();
  361.          }
  362. };
  363. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值