C#是否有一种将双数组转换为类似于C ++ cast.to char *的字符串的方法? [英] Does C# have a way of casting a double array to a string similar to the C++ cast.to a char*?

查看:81
本文介绍了C#是否有一种将双数组转换为类似于C ++ cast.to char *的字符串的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了将双数组转换为char *的C ++代码,如下所示。在C#中,我无法从与C ++强制转换生成的字符串匹配的双精度数组中生成字符串。在C#中,是否有某种方法可以从双数组中生成一个字符串,该字符串与C ++中创建的字符串相匹配,在该字符串中完成了对char *的简单转换?演员的结果似乎是某种二进制数据。



C ++代码:

I have inherited C++ code that casts a double array to a char* as shown below. In C#, I have not been able to generate a string from an array of doubles that matches the string generated by the C++ cast. In C#, is there someway to generate a string from a double array that would match the string created in C++ where a simple cast to char* is done? The result of the cast appears to be some kind of binary data.

C++ code:

char* s = (char*) Darray



其中Darray是一系列双打



我尝试过:



在C#中,我显然无法执行以下操作:




where Darray is an array of doubles

What I have tried:

In C#, I obviously can't do the following:

string theString = (string) Darray





在C#中,我尝试了下面的失败:





In C#, I've tried the following unsuccessfully:

int length = Darray.Length * sizeof(double);
IntPtr pnt = Marshal.AllocHGlobal(length );
Marshal.Copy(Darray, 0, pnt, Darray.Length);
byte[] Barray = new byte[length];
Marshal.Copy(pnt, Barray, 0, length);
string theString = BitConverter.ToString(Barray);





我也尝试过这个失败:



I've also tried this unsuccessfully:

BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream m = new MemoryStream())
{
    formatter.Serialize(m, Darray);
    m.Position = 0;
    StreamReader sr = new StreamReader(m);
    string theString = sr.ReadToEnd();
}



我也尝试过这个失败:


I've also tried this unsuccessfully:

byte[] theBytesData = new byte[numBytesReqd];
Buffer.BlockCopy(Darray, 0, theBytesData, 0, numBytesReqd);
string theString = Encoding.ASCII.GetString(theBytesData, 0, theBytesData.Length);





也许没有解决这个问题的办法。我很感激任何想法。



Maybe there is no solution to this problem. I'd appreciate any thoughts.

推荐答案

在C#中,每个对象都有一个ToString方法,通常应该做一个创建字符串的智能工作。你也有Convert.ToXXX方法强制进行强制转换,并且强制转换也可以工作。



但是看起来你正在尝试使用C#,好像它是C ++一样。看起来一样,但事实并非如此。如果你正在使用指针,你很可能使用C#错误。



如果我有一个双数组变成一个字符串,我想我使用stringbuilder和foreach迭代双打并一次添加一个。在阵列级别没有办法做到这一点。即使在C ++中,你也会获得数字的ASCII版本,而不是数字本身。在C#中,如果这就是你想要的,你就可以了。



In C#, you have a ToString method on every object, which should generally do an intelligent job of creating a string. You also have Convert.ToXXX methods to force a cast, and casting also works.

But it looks like you're trying to use C# as if it was C++. It looks the same, but it's really not. If you're using pointers, odds are good you're using C# wrong.

If I had a double array to turn into a string, I'd imagine I'd use a stringbuilder, and a foreach to iterate over the doubles and add them one at a time. There's no way to do that at the array level. Even in C++, you'd be getting the ASCII versions of the numbers, not the numbers themselves. In C#, if that's what you want, you'd do

Convert.ToChar(xxx); 




每个双倍的
,一次一个,在你的数组中。当然,双倍超过255,所以我仍然困惑你要做的事情。



for each double, one at a time, in your array. Of course, a double goes beyond 255, so I'm still confused what you're trying to do.


这篇关于C#是否有一种将双数组转换为类似于C ++ cast.to char *的字符串的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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