用C#将文本MAC地址生成二进制文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class MAC : Form
{
public MAC()
{
InitializeComponent();
}
[StructLayoutAttribute(LayoutKind.Sequential)] //don’t need
struct ST_EEPROM_MAC
{
public Byte u8MacID0;
public Byte u8MacID1;
public Byte u8MacID2;
// public Byte[] u8MacAdress = new Byte[6];
// public Byte[] u8MacAdress;
public Byte[] u8MacAdress;
public Byte u8MacChecksum;
}
// string CreateMACLogName= "CreateMAC.log";
// string CreateMACLogFilePath = Directory.GetCurrentDirectory();
string CreateMACLogFilePath = ".\\CreateMAC.log";
//Write Log
private void WriteLogFile(ulong MACNumber,int mode)
{
try
{
// CreateMACLogFilePath = CreateMACLogFilePath + CreateMACLogName; //why this method is wrong? can't create file in current file
FileStream CreateLog = new FileStream(CreateMACLogFilePath, FileMode.OpenOrCreate);
string CurrentTime = DateTime.Now.ToString();
string LogRecoder = CurrentTime + "\r\n";
if (1 == mode)
{
LogRecoder = LogRecoder + "Have create " + MACNumber.ToString() + " MAC\r\n";
LogRecoder = LogRecoder + "Start MAC is: " + StartMACByte1.Text + ":" + StartMACByte2.Text + ":" + StartMACByte3.Text + ":" + StartMACByte4.Text + ":" + StartMACByte5.Text + ":" + StartMACByte6.Text + "\r\n";
LogRecoder = LogRecoder + "End MAC is : " + EndMACByte1.Text + ":" + EndMACByte2.Text + ":" + EndMACByte3.Text + ":" + EndMACByte4.Text + ":" + EndMACByte5.Text + ":" + EndMACByte6.Text + "\r\n\r\n\r\n";
}
else if (0 == mode)
{
LogRecoder = LogRecoder + "Have create " + MACNumber.ToString() + " MAC\r\n";
LogRecoder = LogRecoder + "The MAC is:" + Byte1.Text + ":" + Byte2.Text + ":" + Byte3.Text + ":" + Byte4.Text + ":" + Byte5.Text + ":" + Byte6.Text + "\r\n\r\n\r\n";
}
Encoding LogEncoder = Encoding.UTF8;
Byte[] LogBytes = LogEncoder.GetBytes(LogRecoder);
CreateLog.Position = CreateLog.Length;
CreateLog.Write(LogBytes, 0, LogBytes.Length);
CreateLog.Flush();
CreateLog.Close();
return;
}
catch //(System.Exception ex)
{
MessageBox.Show("Write Log failed!");
}
}
private void ChangMacToBtye(ST_EEPROM_MAC MacAddr, Byte[] WriteArray)
{
WriteArray[0] = MacAddr.u8MacID0;
WriteArray[1] = MacAddr.u8MacID1;
WriteArray[2] = MacAddr.u8MacID2;
WriteArray[3] = MacAddr.u8MacAdress[0];
WriteArray[4] = MacAddr.u8MacAdress[1];
WriteArray[5] = MacAddr.u8MacAdress[2];
WriteArray[6] = MacAddr.u8MacAdress[3];
WriteArray[7] = MacAddr.u8MacAdress[4];
WriteArray[8] = MacAddr.u8MacAdress[5];
WriteArray[9] = MacAddr.u8MacChecksum;
}
private void button_CreateMAC_Click(object sender, EventArgs e)
{
if (SingleMAC.Checked == true)
{
SaveFileDialog saveFileDialog;
ST_EEPROM_MAC MacAddr;
MacAddr.u8MacAdress = new Byte[6] { 0, 0, 0, 0, 0, 0 };
Byte u8MacTotalVal = 0;
Byte u8MacLen = 0;
if (Byte1.Text == String.Empty || Byte2.Text == String.Empty || Byte3.Text == String.Empty || Byte4.Text == String.Empty || Byte5.Text == String.Empty || Byte6.Text == String.Empty)
{
MessageBox.Show("Input MAC Error!");
return;
}
if (Convert.ToInt32(Byte1.Text, 16) > 255 || Convert.ToInt32(Byte2.Text, 16) > 255 ||
Convert.ToInt32(Byte3.Text, 16) > 255 || Convert.ToInt32(Byte4.Text, 16) > 255 ||
Convert.ToInt32(Byte5.Text, 16) > 255 || Convert.ToInt32(Byte6.Text, 16) > 255)
{
MessageBox.Show("Input MAC Error!");
return;
}
{
MacAddr.u8MacID0 = 0x55;
MacAddr.u8MacID1 = 0xAA;
MacAddr.u8MacID2 = 0x01;
MacAddr.u8MacAdress[0] = Convert.ToByte(Convert.ToInt32(Byte1.Text, 16));
MacAddr.u8MacAdress[1] = Convert.ToByte(Convert.ToInt32(Byte2.Text, 16));
MacAddr.u8MacAdress[2] = Convert.ToByte(Convert.ToInt32(Byte3.Text, 16));
MacAddr.u8MacAdress[3] = Convert.ToByte(Convert.ToInt32(Byte4.Text, 16));
MacAddr.u8MacAdress[4] = Convert.ToByte(Convert.ToInt32(Byte5.Text, 16));
MacAddr.u8MacAdress[5] = Convert.ToByte(Convert.ToInt32(Byte6.Text, 16));
for (u8MacLen = 0; u8MacLen < 6; u8MacLen++)
{
u8MacTotalVal = (Byte)(u8MacTotalVal + MacAddr.u8MacAdress[u8MacLen]);
}
u8MacTotalVal = (Byte)(0x100 - u8MacTotalVal);
MacAddr.u8MacChecksum = u8MacTotalVal;
saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "bin files(*.bin)|*.bin";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
//saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出MAC地址到";
// DateTime now = DateTime.Now;
saveFileDialog.FileName = "MACADDR.bin";
//点了保存按钮进入
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
/*
if (saveFileDialog.FileName.Trim() == "")
{
MessageBox.Show("请输入要保存的文件名", "提示");
return;
}
progressBar.Visible = true; //进度条 ??
this.panel1.Enabled = false;
* */
}
else
{
// MessageBox.Show("You cancel it!");
return;
}
try
{
FileStream MACfile = new FileStream(saveFileDialog.FileName, FileMode.Create);
int MACAddr_Size = 10;// Marshal.SizeOf(MacAddr); //how to get the size
Byte[] WriteArray = new Byte[MACAddr_Size];
ChangMacToBtye(MacAddr, WriteArray);
/*
WriteArray[0] = MacAddr.u8MacID0;
WriteArray[1] = MacAddr.u8MacID1;
WriteArray[2] = MacAddr.u8MacID2;
WriteArray[3] = MacAddr.u8MacAdress[0];
WriteArray[4] = MacAddr.u8MacAdress[1];
WriteArray[5] = MacAddr.u8MacAdress[2];
WriteArray[6] = MacAddr.u8MacAdress[3];
WriteArray[7] = MacAddr.u8MacAdress[4];
WriteArray[8] = MacAddr.u8MacAdress[5];
WriteArray[9] = MacAddr.u8MacChecksum;*/
//以后再研究这种方式为什么会错!
// IntPtr StructPtr = Marshal.AllocHGlobal(MACAddr_Size);
// Marshal.StructureToPtr(MacAddr,StructPtr,false); //false this step data changed wrong
// Marshal.Copy(StructPtr,WriteArray,0,MACAddr_Size); //wrong
// Marshal.FreeHGlobal(StructPtr);
MACfile.Write(WriteArray, 0, MACAddr_Size);
MACfile.Flush();
MACfile.Close();
WriteLogFile(1,0);
MessageBox.Show("Crate MAC file success!");
}
catch
{
MessageBox.Show("Execption happen!");
}
}
}
//sequential mode
else if (SequentialMAC.Checked == true)
{
SaveFileDialog saveFileDialog;
ST_EEPROM_MAC MacAddr;
MacAddr.u8MacAdress = new Byte[6] { 0, 0, 0, 0, 0, 0 };
Byte u8MacTotalVal = 0;
Byte u8MacLen = 0;
Byte[] TempMACAddr = new Byte[6];
ulong StartMACToUlong = 0, EndMACToUlong = 0,MACCount = 0,Writetimes = 0;
if (StartMACByte1.Text == String.Empty || StartMACByte2.Text == String.Empty || StartMACByte3.Text == String.Empty ||
StartMACByte4.Text == String.Empty || StartMACByte5.Text == String.Empty || StartMACByte6.Text == String.Empty)
{
MessageBox.Show("Input Start MAC Error!");
return;
}
if (Convert.ToInt32(StartMACByte1.Text, 16) > 255 || Convert.ToInt32(StartMACByte2.Text, 16) > 255 ||
Convert.ToInt32(StartMACByte3.Text, 16) > 255 || Convert.ToInt32(StartMACByte4.Text, 16) > 255 ||
Convert.ToInt32(StartMACByte5.Text, 16) > 255 || Convert.ToInt32(StartMACByte6.Text, 16) > 255)
{
MessageBox.Show("Input Start MAC Error!");
return;
}
if (textBox_MACCount.Text == String.Empty)
{
MessageBox.Show("Please input the MAC Number!");
return;
}
/*
if (EndMACByte1.Text == String.Empty || EndMACByte2.Text == String.Empty || EndMACByte3.Text == String.Empty ||
EndMACByte4.Text == String.Empty || EndMACByte5.Text == String.Empty || EndMACByte6.Text == String.Empty)
{
MessageBox.Show("Input End MAC Error!");
return;
}
if (Convert.ToInt32(EndMACByte1.Text, 16) > 255 || Convert.ToInt32(EndMACByte2.Text, 16) > 255 ||
Convert.ToInt32(EndMACByte3.Text, 16) > 255 || Convert.ToInt32(EndMACByte4.Text, 16) > 255 ||
Convert.ToInt32(EndMACByte5.Text, 16) > 255 || Convert.ToInt32(EndMACByte6.Text, 16) > 255)
{
MessageBox.Show("Input End MAC Error!");
return;
}
* */
//get the end MAC
/* //before create bin file depend on start MAC and End MAC
TempMACAddr[0] = Convert.ToByte(Convert.ToInt32(EndMACByte1.Text, 16));
TempMACAddr[1] = Convert.ToByte(Convert.ToInt32(EndMACByte2.Text, 16));
TempMACAddr[2] = Convert.ToByte(Convert.ToInt32(EndMACByte3.Text, 16));
TempMACAddr[3] = Convert.ToByte(Convert.ToInt32(EndMACByte4.Text, 16));
TempMACAddr[4] = Convert.ToByte(Convert.ToInt32(EndMACByte5.Text, 16));
TempMACAddr[5] = Convert.ToByte(Convert.ToInt32(EndMACByte6.Text, 16));
EndMACToUlong = Convert.ToUInt64(TempMACAddr[3]) * 256 * 256 + Convert.ToUInt64(TempMACAddr[4]) * 256 + Convert.ToUInt64(TempMACAddr[5]);
*/
//get the start MAC
TempMACAddr[0] = Convert.ToByte(Convert.ToInt32(StartMACByte1.Text, 16));
TempMACAddr[1] = Convert.ToByte(Convert.ToInt32(StartMACByte2.Text, 16));
TempMACAddr[2] = Convert.ToByte(Convert.ToInt32(StartMACByte3.Text, 16));
TempMACAddr[3] = Convert.ToByte(Convert.ToInt32(StartMACByte4.Text, 16));
TempMACAddr[4] = Convert.ToByte(Convert.ToInt32(StartMACByte5.Text, 16));
TempMACAddr[5] = Convert.ToByte(Convert.ToInt32(StartMACByte6.Text, 16));
StartMACToUlong = Convert.ToUInt64(TempMACAddr[3]) * 256 * 256 + Convert.ToUInt64(TempMACAddr[4]) * 256 + Convert.ToUInt64(TempMACAddr[5]);
//make sure the end mac is bigger than start mac
/*
if (EndMACToUlong<=StartMACToUlong)
{
MessageBox.Show("Please make sure End MAC is bigger than Start MAC");
return;
}
* */
//get MAC number
/*
MACCount = EndMACToUlong - StartMACToUlong+1;
textBox_MACCount.Text = "You have create "+MACCount.ToString()+" MAC address";
* */
MACCount = Convert.ToUInt64(textBox_MACCount.Text);
//show the end MAC
EndMACToUlong = StartMACToUlong + MACCount-1;
TempMACAddr[3] = Convert.ToByte(EndMACToUlong / 256 / 256);
TempMACAddr[4] = Convert.ToByte((EndMACToUlong - Convert.ToUInt64(TempMACAddr[3]) * 256 * 256) / 256);
TempMACAddr[5] = Convert.ToByte(EndMACToUlong % 256);
EndMACByte1.Text = TempMACAddr[0].ToString("X2");
EndMACByte2.Text = TempMACAddr[1].ToString("X2");
EndMACByte3.Text = TempMACAddr[2].ToString("X2");
EndMACByte4.Text = TempMACAddr[3].ToString("X2");
EndMACByte5.Text = TempMACAddr[4].ToString("X2");
EndMACByte6.Text = TempMACAddr[5].ToString("X2");
saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "bin files(*.bin)|*.bin";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
//saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出MAC地址到";
DateTime now = DateTime.Now;
saveFileDialog.FileName = "MACADDR.bin";
//点了保存按钮进入
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
/*
if (saveFileDialog.FileName.Trim() == "")
{
MessageBox.Show("请输入要保存的文件名", "提示");
return;
}
* */
}
else
{
return;
}
try
{
int MACAddr_Size = 10;// use Marshal.SizeOf(MacAddr); get the size is wrong //how to get the size
Byte[] WriteArray = new Byte[MACAddr_Size];
FileStream MACfile = new FileStream(saveFileDialog.FileName, FileMode.Create);
//将MAC地址写入到Bin文件中
for (Writetimes = 0; Writetimes < MACCount; Writetimes++)
{
MacAddr.u8MacID0 = 0x55;
MacAddr.u8MacID1 = 0xAA;
MacAddr.u8MacID2 = 0x01;
MacAddr.u8MacAdress[0] = TempMACAddr[0];
MacAddr.u8MacAdress[1] = TempMACAddr[1];
MacAddr.u8MacAdress[2] = TempMACAddr[2];
MacAddr.u8MacAdress[3] = Convert.ToByte(StartMACToUlong/256/256);
MacAddr.u8MacAdress[4] = Convert.ToByte((StartMACToUlong - Convert.ToUInt64(MacAddr.u8MacAdress[3]) * 256 * 256) / 256);
MacAddr.u8MacAdress[5] = Convert.ToByte(StartMACToUlong%256);
for (u8MacLen = 0; u8MacLen < 6; u8MacLen++)
{
u8MacTotalVal = (Byte)(u8MacTotalVal + MacAddr.u8MacAdress[u8MacLen]);
}
u8MacTotalVal = (Byte)(0x100 - u8MacTotalVal);
MacAddr.u8MacChecksum = u8MacTotalVal;
ChangMacToBtye(MacAddr, WriteArray);
MACfile.Write(WriteArray, 0,MACAddr_Size);
StartMACToUlong++;
}
MACfile.Flush();
MACfile.Close();
WriteLogFile(MACCount,1);
MessageBox.Show("Create MAC success");
}
catch
{
MessageBox.Show("Execption happen!");
}
}
else
{
MessageBox.Show("Please choose a mode!");
}
}
private void Byte1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void Byte2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void Byte3_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void Byte4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void Byte5_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void Byte6_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void StartMACByte6_TextChanged(object sender, EventArgs e)
{
}
private void StartMACByte5_TextChanged(object sender, EventArgs e)
{
}
private void StartMACByte4_TextChanged(object sender, EventArgs e)
{
}
private void Byte6_TextChanged(object sender, EventArgs e)
{
}
private void SingleMAC_CheckedChanged(object sender, EventArgs e)
{
if (SingleMAC.Checked == true)
{
if (SequentialMAC.Checked == true)
{
MessageBox.Show("You have choosed the sequential mode!");
SingleMAC.Checked = false;
}
}
}
private void SequentialMAC_CheckedChanged(object sender, EventArgs e)
{
if (SequentialMAC.Checked == true)
{
if (SingleMAC.Checked == true)
{
MessageBox.Show("You have choosed the single mode!");
SequentialMAC.Checked = false;
}
}
}
private void SingleMAC_CheckStateChanged(object sender, EventArgs e)
{
}
private void Byte3_TextChanged(object sender, EventArgs e)
{
}
private void StartMACByte1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void StartMACByte2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void StartMACByte3_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void StartMACByte4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void StartMACByte5_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void StartMACByte6_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte3_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte5_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte6_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58))
|| ((e.KeyChar > 64) && (e.KeyChar < 71)) || ((e.KeyChar > 96) && (e.KeyChar < 103)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void textBox_MACCount_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar == 8) || ((e.KeyChar > 47) && (e.KeyChar < 58)))
{
//
}
else
{
// MessageBox.Show("Wrong input!");
e.Handled = true;
}
}
private void EndMACByte1_TextChanged(object sender, EventArgs e)
{
if (EndMACByte1.Text != StartMACByte1.Text)
{
MessageBox.Show("Must be same with the start MAC");
EndMACByte1.Text = StartMACByte1.Text;
}
}
private void EndMACByte2_TextChanged(object sender, EventArgs e)
{
if (EndMACByte2.Text != StartMACByte2.Text)
{
MessageBox.Show("Must be same with the start MAC");
EndMACByte2.Text = StartMACByte2.Text;
}
}
private void EndMACByte3_TextChanged(object sender, EventArgs e)
{
if (EndMACByte3.Text != StartMACByte3.Text)
{
MessageBox.Show("Must be same with the start MAC");
EndMACByte3.Text = StartMACByte3.Text;
}
}
private void StartMACByte1_TextChanged(object sender, EventArgs e)
{
EndMACByte1.Text = StartMACByte1.Text;
}
private void StartMACByte2_TextChanged(object sender, EventArgs e)
{
EndMACByte2.Text = StartMACByte2.Text;
}
private void StartMACByte3_TextChanged(object sender, EventArgs e)
{
EndMACByte3.Text = StartMACByte3.Text;
}
}
}
使用C#将字符串生成二进制文件
要使用C#将字符串生成二进制文件,你可以使用System.IO命名空间中的FileStream类来实现。以下是一个简单的示例代码,演示了如何将字符串写入二进制文件:
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
// 要写入的字符串
string inputString = “这是一个示例字符串。”;
// 文件路径和名称
string filePath = "output.bin";
// 使用FileStream创建一个二进制文件
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
// 将字符串转换为字节数组
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
// 将字节数组写入文件
fileStream.Write(bytes, 0, bytes.Length);
Console.WriteLine("字符串已成功写入二进制文件。");
}
}
}
上述代码将一个字符串转换为UTF-8编码的字节数组,然后使用FileStream将字节数组写入二进制文件。确保替换inputString和filePath以匹配你的需求。运行此代码后,将在指定的文件路径中创建一个名为output.bin的二进制文件,其中包含了字符串的二进制表示。
为什么用C#来开发这个简单的上位机
之前开发一些简单的上位机一般都是用的MFC,这次用了下C#确实简单方便很多。
使用C#来开发上位机(通常是指用于与嵌入式系统、传感器、设备或其他硬件进行通信和数据处理的计算机应用程序)具有许多优势,其中一些包括:
易学易用:C#是一种现代的、高级的编程语言,其语法和结构与人类自然语言更加接近,因此相对容易学习和使用。它具有强大的开发工具和社区支持,有助于提高开发效率。
强大的生态系统:C#是.NET平台的一部分,拥有庞大而丰富的生态系统。你可以轻松地使用各种库、框架和工具,从图形界面到数据访问,再到网络通信,来加速应用程序的开发过程。
跨平台开发:借助.NET Core(现在称为.NET 5及更高版本),你可以开发跨平台的C#应用程序,这意味着你可以在Windows、Linux和macOS等不同操作系统上运行相同的应用程序。
强类型语言:C#是一种强类型语言,这意味着它在编译时会检查类型安全性,从而减少了运行时错误的可能性,提高了代码的可靠性和可维护性。
大量的第三方库和NuGet包:C#生态系统拥有大量的第三方库和NuGet包,可帮助你轻松地集成各种功能,从图形用户界面(GUI)到通信协议和数据分析。
多线程和异步编程:C#提供了强大的多线程和异步编程支持,这对于实时数据处理和并发任务非常重要。
良好的集成性:C#可以轻松地与各种数据库、云服务和其他系统集成,以便处理数据和进行通信。
安全性:C#具有一些内置的安全功能,如代码签名和代码访问安全性,可帮助保护应用程序免受潜在的恶意代码攻击。
活跃的社区和支持:C#拥有一个活跃的开发者社区,你可以在社区中找到答案、解决问题,并获得开发支持。文章来源:https://www.toymoban.com/news/detail-541812.html
总结
C#是一个功能强大且易于使用的编程语言,适用于开发上位机应用程序,尤其在与.NET平台的集成和跨平台支持方面表现出色。这些优势使C#成为许多工程师和开发者的首选语言之一,用于构建与硬件设备通信和数据处理相关的应用程序。文章来源地址https://www.toymoban.com/news/detail-541812.html
到了这里,关于C#生成二进制文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!