需要帮助将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
?
缺少一些声明使直译变得不确定,但是您只需要一些想法:
- 要表示位集,请使用
System.Collections.BitArray
,
请参见 http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx [ http://msdn.microsoft.com/en-us/library/system.array. aspx [^ ],
http://msdn.microsoft.com/en-us/library/z50k9bft.aspx [^ ].
- 其他所有内容都相同:
if
语句,阻止…
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 isais_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…