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#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
}
}
}