Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from 唯世横刀/MVS
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (2)
master
dev1.1
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (2)
master
dev1.1
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (2)
master
dev1.1
MVS
/
VisionBasicSystem
/
Commons
/
ByteUtils.cs
MVS
/
VisionBasicSystem
/
Commons
/
ByteUtils.cs
ByteUtils.cs 8.37 KB
Copy Edit Raw Blame History
雷涛 authored 2019年12月28日 17:07 +08:00 . 【数据传输】实现与PLC数据传输
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
using System;
using System.Text;
using System.Collections;
namespace VisionBasicSystem
{
internal class ByteUtils
{
//字符串拆分
public static ArrayList Str16ToArrayList(string strIn)
{
string sParse = "";
ArrayList myAL = new ArrayList();
int i = 0;
foreach (char cc in strIn)
{
i++;
if (cc == ' ' || sParse.Length == 2)
{
myAL.Add(sParse);
if (sParse.Length == 2 && cc != ' ')//两个字符
{
sParse = Convert.ToString(cc);
}
else
{
sParse = "";
}
}
else
{
sParse += Convert.ToString(cc);
if (i == strIn.Length && cc != ' ')//末尾字符
{
myAL.Add(sParse);
}
}
}
return myAL;
}
//以两个或空格问区分字符串增加空格
public static string StrAddSpace(string strInData)
{
string strParse = "";
string strAddResult = "";
int i = 0;
foreach (char cc in strInData)
{
i++;
if (cc == ' ' || strParse.Length == 2)
{
strAddResult += strParse + " ";
//两个字符
if (strParse.Length == 2 && cc != ' ')
{
strParse = Convert.ToString(cc);
}
else
{
strParse = "";
}
}
else
{
strParse += Convert.ToString(cc);
}
//末尾字符
if (i == strInData.Length && cc != ' ')
{
strAddResult += strParse;
}
}
return strAddResult;
}
//去除字符串中空格并把单个字符串前面加0
public static string StrSubSpace(string strInData)
{
//将头和尾组合成一样的形式
string strParse = " " + strInData + " ";
string strParseTemp = "";
string strSubResult = "";
//int i = 0;
for (int i = 0; i < strParse.Length; i++)
{
//char myChar
/*if ((i == 0) && (strParse[1]==' '))
{
strParseTemp = "0" + strParse[0].ToString();
strSubResult += strParseTemp;
}
else*/
if ((i > 0) && (i < strInData.Length + 1))
{
if ((strParse[i] != ' ') && (strParse[i - 1] == ' ') && (strParse[i + 1] == ' '))
{
strParseTemp = "0" + strParse[i].ToString();
strSubResult += strParseTemp;
}
else
{
strParseTemp = strParse[i].ToString();
strSubResult += strParseTemp;
}
}
}
strSubResult = strSubResult.Replace(" ", "");
return strSubResult;
}
/// <summary>
/// 十六进制数组形式的CRC—A001
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] CRC_16_A001(byte[] data)
{
//int len = data.Length;
//ushort tt;
if (data.Length > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < data.Length; i++)
{
crc = (ushort)(crc ^ (data[i]));
for (int j = 0; j < 8; j++)
{
#region
//也可以用功能相同
/*tt = (ushort)(crc & 1);
crc = (ushort)(crc >> 1);
crc = (ushort)(crc & 0x7fff);
if (tt == 1)
{
crc = (ushort)(crc ^ 0xa001);
}
crc = (ushort)(crc & 0xffff);*/
#endregion
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
}
}
byte hi = (byte)((crc & 0xFFFF) >> 8);//高位置
byte lo = (byte)(crc & 0xFFFF);//低位置
return new byte[] { hi, lo };
}
return new byte[] { 0, 0 };
}
public static byte CS(byte[] dataIn)//十六进制数组形式的CS
{
byte resultVal = 0;
for (int i = 0; i < dataIn.Length; i++)
{
resultVal += dataIn[i];
}
return resultVal;
}
//ASCII字符串转换为Hex字节
public static byte[] StrAsciiToHexBy(string inStr)
{
//string s = "";
ArrayList al = Str16ToArrayList(inStr);
byte[] by = new byte[al.Count];
int i = 0;
foreach (string stmp in al)
{
//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
by[i] += Convert.ToByte(stmp, 16);
i++;
}
//s = Encoding.GetEncoding("Gb2312").GetString(by);//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
return by;//s;
}
//ASCII字符串转换为Hex字符串
public static string StrAsciiToHexStr(string inStr)
{
string s = "";
ArrayList al = Str16ToArrayList(inStr);
byte[] by = new byte[al.Count];
int i = 0;
foreach (string stmp in al)
{
//将指定基的数字的字符串表示形式转换为等效的 8 位无符号整数。
by[i] += Convert.ToByte(stmp, 16);
i++;
}
//在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。。
s = Encoding.GetEncoding("Gb2312").GetString(by);
return s;
}
public static byte ByLength(byte[] by)//总长度
{
byte myBy;
myBy = Convert.ToByte(by.Length);
return myBy;
}
//总长度
public static Int16 int16Length(byte[] by)
{
Int16 myBy;
myBy = Convert.ToByte(by.Length);
return myBy;
}
/// 将一条十六进制字符串转换为ASCII
/// </summary>
/// <param name="hexstring">一条十六进制字符串</param>
/// <returns>返回一条ASCII码</returns>
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
/// <summary>
/// 16进制字符串转换为二进制数组
/// </summary>
/// <param name="hexstring">字符串每个字节之间都应该有空格,大多数的串口通讯资料上面的16进制都是字节之间都是用空格来分割的。</param>
/// <returns>返回一个二进制字符串</returns>
public static byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/VisionDeveloper/MVS.git
git@gitee.com:VisionDeveloper/MVS.git
VisionDeveloper
MVS
MVS
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /