Ascii字节数组转换为Int32或Double [英] Ascii Bytes Array To Int32 or Double

查看:469
本文介绍了Ascii字节数组转换为Int32或Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写一个具有完全授权可以免费分配的图书馆.目标是在应用程序的启动阶段完成后,拥有0个收藏夹.

I'm re-writing alibrary with a mandate to make it totally allocation free. The goal is to have 0 collections after the app's startup phase is done.

以前,有很多这样的电话:

Previously, there were a lot of calls like this:

Int32 foo = Int32.Parse(ASCIIEncoding.ASCII.GetString(bytes, start, length));

我相信这是分配一个字符串.我找不到可以自动执行相同操作的C#库函数.我看了看BitConverter类,但是只有当您的Int32用表示它的实际字节编码时,它才看起来像.在这里,我有一个字节数组,它们表示代表Int32的Ascii字符.

Which I believe is allocating a string. I couldn't find a C# library function that would do the same thing automatically. I looked at the BitConverter class, but it looks like that is only if your Int32 is encoded with the actual bytes that represent it. Here, I have an array of bytes representing Ascii characters that represent an Int32.

这就是我所做的

public static Int32 AsciiBytesToInt32(byte[] bytes, int start, int length)
{
     Int32 Temp = 0;
     Int32 Result = 0;
     Int32 j = 1;

     for (int i = start + length - 1; i >= start; i--)
     {
          Temp = ((Int32)bytes[i]) - 48;

          if (Temp < 0 || Temp > 9)
          {
               throw new Exception("Bytes In AsciiBytesToInt32 Are Not An Int32");
          }

          Result += Temp * j;
          j *= 10;
     }

     return Result;
}

有人知道C#库函数已经以更好的方式做到了吗?或是为了使上述操作更快而进行的一项改进(可能在一天之内被称为数百万次).谢谢!

Does anyone know of a C# library function that already does this in a more optimal way? Or an improvement to make the above run faster (its going to be called millions of times during the day probably). Thanks!

推荐答案

每天数百万次应该不是问题-我希望它每秒能够运行数十万次.就我个人而言,我将重写上面的内容,以便仅在循环内声明"temp"(并摆脱Pascal-cases局部变量名urgh),但这应该可以.

Millions of times per day shouldn't be a problem - I'd expect that to be able to run hundreds of thousands of times per second. Personally I'd rewrite the above to only declare "temp" within the loop (and get rid of the Pascal-cases local variable names - urgh) but it should be okay.

该代码将更容易理解为:

The code would be more immediately understandable as:

int digit = bytes[i] - '0';

与您的

Temp = ((Int32)bytes[i]) - 48;

行,但以更简单的方式(IMO).它们的行为应完全相同.

line, but in a simpler way (IMO). They should behave exactly the same way.

总的来说,尝试在不进行任何分配的情况下编写C#是很严厉的,并且与语言和框架的设计方法背道而驰.您是否认为这实际上是合理的要求?诚然,我听说过这是一些使用托管代码编写游戏的方式……但这似乎有些奇怪.

On a general note, trying to write C# without any allocations is pretty harsh, and fights against the way the language and framework are designed. Do you believe this is actually a reasonable requirement? Admittedly I've heard about it being the way some games are written in managed code... but it does seem a bit odd.

当然,如果字节不合适,您将分配一个例外...

Of course, you're going to allocate an exception if the bytes are inappropriate...

请注意,您的代码不允许使用负数.可以吗?

Note that your code doesn't allow for negative numbers. Is that okay?

这篇关于Ascii字节数组转换为Int32或Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆