我把16进制发送用到的函数放在这里,后期再回来加上
- ///<字符串转16进制格式,不够自动前面补零>//////</summary>///<param name="hexString"></param>///<returns></returns>privatestaticbyte[] strToToHexByte(String hexString)
- {
- int i;
- boolFlag =false;
- hexString = hexString.Replace("","");//清除空格if((hexString.Length %2) !=0)
- {
- Flag =true;
- }
- if(Flag ==true)
- {
- byte[] returnBytes =newbyte[(hexString.Length +1) /2];
- try {
- for(i =0; i < (hexString.Length -1) /2; i++)
- {
- returnBytes[i] = Convert.ToByte(hexString.Substring(i *2,2),16);
- }
- returnBytes[returnBytes.Length -1] = Convert.ToByte(hexString.Substring(hexString.Length -1,1).PadLeft(2,'0'),16);
- }
- catch {
- for(i =0; i < returnBytes.Length; i++)
- {
- returnBytes[i] =0;
- }
- MessageBox.Show("超过16进制范围A-F,已初始化为0","提示");
- }
- return returnBytes;
- }
- else {
- byte[] returnBytes =newbyte[(hexString.Length) /2];
- try {
- for(i =0; i < returnBytes.Length; i++)
- {
- returnBytes[i] = Convert.ToByte(hexString.Substring(i *2,2),16);
- }
- }
- catch {
- for(i =0; i < returnBytes.Length; i++)
- {
- returnBytes[i] =0;
- }
- MessageBox.Show("超过16进制范围A-F,已初始化为0","提示");
- }
- return returnBytes;
- }
- }
|