需要帮助将C ++代码转换为C# [英] Need help converting C++ code to C#

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

问题描述

我需要将以下代码片段转换为C#,这怎么可能?

I need to convert the following code snippet into C#, how is this possible?

char south=ais_binary[89];
    temp_s=ais_binary.substr(89,27);
    bitset<27> ii(temp_s);
    if (south=='1') {ii.flip();}
    double ais_latitude=ii.to_ulong();
    ais_latitude=ais_latitude/10000/60;
    if (south=='1') {ais_latitude *= -1;}



在此先感谢



Thanks in advance

推荐答案

首先,即使是C ++,代码也不太好:
1)使用所有这些立即数(89、27,"1"等)使代码很难维护; 2)与字符"1"进行比较是表示纯逻辑的不好方法,3)未显示一些声明:ais_binary在哪里(可能是某些实现索引的类的实例),temp_s?

缺少一些声明使直译变得不确定,但是您只需要一些想法:

First of all, the code is not so good even as C++:
1) use of all those immediate constants (89, 27, ''1'', etc.) makes the code very hard to maintain; 2) comparison with character ''1'' is bad way to represent pure logic, 3) some declarations are not shown: where is ais_binary (could be an instance of some class implementing indexing), temp_s?

The lack of some declarations makes literal translation uncertain, but you only need some ideas:

  • To represent bit set, use System.Collections.BitArray,
    see http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx[^].
  • To get a slice of some array use the static methods <copy>System.Array.Copy. See: http://msdn.microsoft.com/en-us/library/system.array.aspx[^],
    http://msdn.microsoft.com/en-us/library/z50k9bft.aspx[^].
  • Everything else looks the same: if statement, block…


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

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