将C中的负索引转换为C# [英] Convert negative index in C into C#

查看:78
本文介绍了将C中的负索引转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have to convert my code from c with pointers into c#
code in C is like this 

myMethodName(Word16 exc[],Word16 T0)
{
  Word16 *p_exc;
  Word16 *a;
  a=&(exc[-T0])
  a++;
  b=a;
  c=a;
  a++;
  p_exc = exc;
for (i = 10; i > 0; i--)
{
	s2 += ((Word32) * (b--));
	s1 += ((Word32) * (b));
        s1 += ((Word32) * (c++));
        s2 += ((Word32) * (c));
}
*(p_exc++) = (Word16)(s1 >> 15);
}





我的尝试:





What I have tried:

I Have tried this
myMethodName(Int16[] exc,Int16 T0)
{
 Int16 pIndex=0;
 Int16 a=0;
 a = T0;
a++;
b=a;
c=a;
for (i = 10; i > 0; i--)
{
	s2 += ((Int32) * (b--));
	s1 += ((Int32) * (b));
        s1 += ((Int32) * (c++));
        s2 += ((Int32) * (c));
}
p_exc[pIndex++] = (Int16)(s1 >> 15);
}

推荐答案

如果不使用不安全 keyword - 不建议在没有充分理由的情况下在C#中使用指针,并且 unsafe 关键字(和构建选项)是强制执行的。

你的C#代码与C原代显着不同,以确保即使使用 unsafe 和使用指针,它也不会做同样的工作而没有重要意义也改变了外部代码。



一般来说,从一种语言转换到另一种语言不会产生好的代码,并且从使用负面索引的东西开始,如果可能的话从来没有好好工作。

使用C代码作为指导,并解决为什么需要这样的索引,你会得到更好的服务。然后设计没有的C#代码 - 结果会得到更多可维护的代码。
That won't compile without the use of the unsafe keyword - it is not recommended to use pointers in C# without very good reason and the unsafe keyword (and build option) are there to enforce that.
And your C# code is significantly different from the C original to ensure that even with unsafe and the use of pointers, it won't do the same job without significant changes to the external code as well.

Generally speaking, converting from one language to another does not result in "good code" and starting from something using negative indexes if probably never togin to work well.
You would be much better served by using the C code as a guide, and working out why it needs such indexes. Then designing C# code that doesn't - you will get much more maintainable code as a result.


Quote:

将C中的负索引转换为C#

Convert negative index in C into C#



基本上,负索引不存在。

您需要了解似乎有一个基础结构负指数实际上它是一个带有另一个基地址的正数。



你需要分析你的应用程序以了解它是什么。


Basically, negative indexes do not exist.
You need to understand the underlying structure which appears to have a negative index when in fact it is a positive one with another base address.

You need to analyze your app to understand what is what.

这篇关于将C中的负索引转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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