高分求C# 的LCR校验码计算函数 如题~分不够再给。有能力的情况下给分给到满意为止~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用一个Timer可以了,根据当前播放的时间读取歌词文件中的每行开头的时间,时间比对一致的话就显示该行歌词就行了。 当然Timer的Interval要设置的间隔小一点喽 4楼的兄弟~我说的LCR并不是歌词~而是modbus协议中使用的校验码算法。 public string LRC(string str) { int d_lrc = 0; string h_lrc = ""; int l = str.Length; for (int c = 0; c < l; c = c + 2) { string c_data = str.Substring(c, 2); d_lrc = d_lrc +Convert.ToInt32(c_data); } if (d_lrc >= 16) d_lrc = d_lrc%16; h_lrc = Convert.ToInt32(~d_lrc + 1).ToString("X"); if (h_lrc.Length > 2) h_lrc = h_lrc.Substring (h_lrc .Length -2,2); return h_lrc; }读书的时候记得,现在都忘了,这是网上找的,你看看对不 static byte LRC(byte[] data) { byte lrc = 0; foreach (byte c in data) { lrc += c; } return (byte)-lrc; } public static string checkSum(string writeUncheck) { char[] hexArray = new char[writeUncheck.Length]; hexArray = writeUncheck.ToCharArray(); int decNum = 0, decNumMSB= 0, decNumLSB = 0; int decByte, decByteTotal = 0; bool msb = true; for( int t = 0; t <= hexArray.GetUpperBound(0); t++) { if((hexArray[t]>= 48) && (hexArray[t] <= 57)) decNum = (hexArray[t] - 48); else if ( (hexArray[t] >= 65) & (hexArray[t] <= 70)) decNum = 10 + (hexArray[t] - 65); if (msb) { decNumMSB = decNum * 16; msb = false; } else { decNumLSB = decNum; msb = true; } if (msb) { decByte = decNumMSB + decNumLSB; decByteTotal += decByte; } } decByteTotal = (255 - decByteTotal) + 1; decByteTotal = decByteTotal & 255; int a, b = 0; string hexByte = "", hexTotal = ""; double i; for (i = 0; decByteTotal > 0; i++) { b = Convert.ToInt32(System.Math.Pow(16.0, i)); a = decByteTotal % 16; decByteTotal /= 16; if (a <= 9) hexByte = a.ToString(); else { switch (a) { case 10: hexByte = "A"; break; case 11: hexByte = "B"; break; case 12: hexByte = "C"; break; case 13: hexByte = "D"; break; case 14: hexByte = "E"; break; case 15: hexByte = "F"; break; } } hexTotal = String.Concat(hexByte, hexTotal); } return hexTotal; }问题基本上解决,但还是有点问题,这算法是在codeproject找的。还是有时会计算错,不过比我之前的少。不知道什么回来。 该文件被另一进程使用 无法删除。。。 有一条写在类里面的SQL语句,个为帮忙看看哪里有错。 将emf保存到流中时出错 求助!c#中关于文件处理的问题 发送邮件错误 读取字符串? VC 6.0 中可以调用.net的控件吗? 请问如何将EXCEL中的数据用循环逐一导入到DataTable或DataSet中 C#与C++.net有什么不同啊? win8和windows server 2012哪个作C#开发的操作系统比较合适 C# 多线程编程 C# 编程路怎么走?
当然Timer的Interval要设置的间隔小一点喽
public string LRC(string str)
{
int d_lrc = 0;
string h_lrc = "";
int l = str.Length;
for (int c = 0; c < l; c = c + 2)
{
string c_data = str.Substring(c, 2);
d_lrc = d_lrc +Convert.ToInt32(c_data);
}
if (d_lrc >= 16)
d_lrc = d_lrc%16;
h_lrc = Convert.ToInt32(~d_lrc + 1).ToString("X");
if (h_lrc.Length > 2)
h_lrc = h_lrc.Substring (h_lrc .Length -2,2);
return h_lrc;
}读书的时候记得,现在都忘了,这是网上找的,你看看对不
{
byte lrc = 0;
foreach (byte c in data)
{
lrc += c;
}
return (byte)-lrc;
}
public static string checkSum(string writeUncheck)
{
char[] hexArray = new char[writeUncheck.Length];
hexArray = writeUncheck.ToCharArray();
int decNum = 0, decNumMSB= 0, decNumLSB = 0;
int decByte, decByteTotal = 0; bool msb = true;
for( int t = 0; t <= hexArray.GetUpperBound(0); t++)
{
if((hexArray[t]>= 48) && (hexArray[t] <= 57))
decNum = (hexArray[t] - 48);
else if ( (hexArray[t] >= 65) & (hexArray[t] <= 70))
decNum = 10 + (hexArray[t] - 65); if (msb)
{
decNumMSB = decNum * 16;
msb = false;
}
else
{
decNumLSB = decNum;
msb = true;
}
if (msb)
{
decByte = decNumMSB + decNumLSB;
decByteTotal += decByte;
}
} decByteTotal = (255 - decByteTotal) + 1;
decByteTotal = decByteTotal & 255; int a, b = 0;
string hexByte = "", hexTotal = "";
double i; for (i = 0; decByteTotal > 0; i++)
{
b = Convert.ToInt32(System.Math.Pow(16.0, i));
a = decByteTotal % 16;
decByteTotal /= 16;
if (a <= 9)
hexByte = a.ToString();
else
{
switch (a)
{
case 10:
hexByte = "A";
break;
case 11:
hexByte = "B";
break;
case 12:
hexByte = "C";
break;
case 13:
hexByte = "D";
break;
case 14:
hexByte = "E";
break;
case 15:
hexByte = "F";
break;
}
}
hexTotal = String.Concat(hexByte, hexTotal);
}
return hexTotal;
}问题基本上解决,但还是有点问题,这算法是在codeproject找的。还是有时会计算错,不过比我之前的少。不知道什么回来。