Pla*_*pus 5 .net c# vb.net string
在低延迟HFT应用程序中,我有套接字接收Byte[]包含股票市场数据的消息.
在供应商协议中,股票价格字段Byte[]包含ASCII字符序列.
即字节序列[51-51-46-56]对应于ascii字符[3-3 -.- 8],等于33.8数字.
实际上在消息解析期间我转换Byte[]为字符串然后字符串为Single/float.
有没有办法避免这种双重转换并直接转换Byte[]为单个/浮点数?
其次是否有一种方法可以避免使用在.NET中效率低下的字符串类型(容易发生不可变垃圾收集)?
提前致谢.
问候
[编辑2019年]:这是3年来完美无缺的最终解决方案:
/// <summary> Read a Double precision float field in GL format from the stream and advances stream position of n (1 of field lenght flag + field lenght). </summary>
/// <returns>-1 in case of error, Double precision floating point value ROUNDED TO 8 DECIMAL PLACES in case of success </returns>
/// <param name="IniPos"> Initial Stream Position</param>
public double ReadGLFieldAsDouble(long IniPos = -1)
{
// --
if (IniPos >= 0)
Strm.Position = IniPos;
int FLenght = Strm.ReadByte - 32; // case of "01000" SW Snapshot, field 7 doesn't exists, but in the message is a blank (32)
// --early exit
if (FLenght <= 0)
return -1; // a blank field (byte 32) returns -1
if (Strm.Length - Strm.Position < FLenght)
return -1;
// --
double Dbl = 0;
int Cpos = -1;
int b = 0;
int sign = 1;
// --Builds as Integer with no point Separator
for (int i = 0; i < FLenght ; i++)
{
b = Strm.ReadByte;
switch (b)
{
case 45: // ASCII 45="-" sign
{
sign = -1;
break;
}
case 46: // ASCII 46 is decimal point="." ; ASCII 44 is comma=","
{
Cpos = i; // decimal point position
break;
}
default:
{
Dbl = Dbl * 10 + (b - 48); // increments as integer ASCII 48=0
break;
}
}
}
// --Transforms in floating point dividing by power of 10, multiplies by sign and returns
if (Cpos != -1)
Dbl = (Dbl / (Math.Pow(10, (FLenght - 1 - Cpos))));
return Math.Round(sign * Dbl, 8);
}
Run Code Online (Sandbox Code Playgroud)
byte[] arr = new byte[] { 51, 51, 46, 56};
double res = 0, floatindex = 0.1;
bool isFraction = false;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 46)
{
isFraction = true;
continue;
}
if (!isFraction)
res = 10*res + arr[i] - 48;
else
{
res += (arr[i] - 48)*floatindex;
floatindex /= 10.0;
}
}
return res;
Run Code Online (Sandbox Code Playgroud)
从文化的角度来看,它假设你总是有一个'点'来标记小数点的分隔(而不是像法国文化中那样的昏迷)
| 归档时间: |
|
| 查看次数: |
103 次 |
| 最近记录: |