You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommService
{
/*
*Utility
*
*2010-10-30
*
*/
class Utility
{
/*
*floatStringToBCD
*
*2010-10-30
*string sFloat ,
*int nIntSize ,
*int nFloat ,
*ref byte[] arrBytes
*bool true false
*BCD
*/
public static bool floatStringToBCD(string sFloat,int nIntSize,int nFloat, ref byte[] arrBytes)
{
if (sFloat == "" || (nIntSize + nFloat)%2 > 0)
{
return false;
}
string sLeft;
string sRight;
int nFirstPos = sFloat.IndexOf('.');
int nOldLen = sFloat.Length;
int nPos = nFirstPos;
while (nPos >= 0)
{
sFloat = sFloat.Remove(nPos, 1);
nPos = sFloat.IndexOf('.');
}
if (nFirstPos == 0)
{
sLeft = new string('0', nIntSize);
sRight = sFloat;
}
else if (nFirstPos == -1 || nFirstPos == (nOldLen - 1))
{
sLeft = sFloat;
sRight = new string('0', nFloat);
}
else
{
sLeft = sFloat.Substring(0, nFirstPos);
sRight = sFloat.Substring(nFirstPos, sFloat.Length - nFirstPos);
}
sLeft = sLeft.PadLeft(nIntSize, '0');
sRight = sRight.PadRight(nFloat,'0');
string sOK = sLeft.Substring(sLeft.Length - nIntSize, nIntSize) + sRight.Substring(0, nFloat);
arrBytes = new byte[sOK.Length / 2];
for (int i = 0; i < sOK.Length / 2; i++)
{
arrBytes[i] = Convert.ToByte(sOK.Substring(2 * i, 2), 16);
}
return true;
}
}
}