Click here to Skip to main content
14,737,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have inherited existing C++ code that converts an array of doubles into a string, writes it to a database memo field, and then can read the string from the database memo field and convert it to an array of doubles. I want to write c# code that does the same operation of converting an array of doubles into a string such that it is compatible with the C++ code that converts it from a string to a double array. What is the proper c# code to produce the same string?

C++ code that converts the double array to a string:

int numValues = 3;
double stations[] = { 1.0,2.0,3.0 };
int blockSize = numValues * sizeof(double);
char* mp_memoValue = new char[blockSize + 1];
memcpy(mp_memoValue, (char*)stations, blockSize);

What I have tried:

byte[] theBytesData = new byte[numBytesReqd];
Buffer.BlockCopy(stations, 0, theBytesData, 0, numBytesReqd);
theMemoField = Encoding.ASCII.GetString(theBytesData, 0, theBytesData.Length);

Also, following up on proposed solution #1, I did the following although it didn't produce the same binary string that the C++ code produces.

The idea of using the binary copy seems correct. Maybe the basic question is, what does (char*)stations do in C++ and is it possible to duplicate that behavior in C# and then do the binary copy? I may be asking the impossible, my knowledge is limited.

BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream m = new MemoryStream())
    formatter.Serialize(m, stations);
    m.Position = 0;
    StreamReader sr = new StreamReader(m);
    theMemoField = sr.ReadToEnd();
Updated 22-Apr-19 10:34am

You didn't show us what the string created by the C++ code looks like.

I'm going to assume (since you didn't specify) that your string of doubles is delimited in some way, probably with a comma.

I'm going to further assume that you've retrieved the string from the database.

Given those two assumptions...

To put an array of doubles into a delimited string:

// convert an array of doubles to a deliimited string
string myData = string.Empty;
double[] doubles = new double[]{1.2,3.4,5.6 };
foreach(double dbl in doubles)
    // we only need a comma if the string is not empty
    string comma = (myData.Length == 0)?"":",";
    // convert the value to a string (you can also use TryParse if the data is not trusted to be correct)
    string value = string.Format("{0:#.00}", dbl);
    // add it to the string
    myData = string.Format("{0}{1}{2}", myData, comma, value);
// at this point myData should be "1.20,3.40,5.60"


// now we convert the string back to an array of doubles
// split the string on the delimiter
string[] parts = myData.Split(',');
// create a double array of the same length
double[] partsDouble = new double[parts.Length];

// loop through the delimited string parts,convert the values to doubles, and 
// assign them to the approriate double array index
for (int i = 0; i < parts.Length; i++)
    partsDouble[i] = Convert.ToDouble(parts[i]);

You could get really fancy with and use Linq for this, but usually, maintainability with clear-cut code is a better solution. Besides that, sometimes it's all about completing the task with as little fuss as possible.
Member 14077625 22-Apr-19 15:12pm
The string of doubles is not delimited. It's just a double array and the original C++ code cast it to a char* and then did a memcpy into a string before writing to the database. If you look at the string that's stored in the database or look at it in the IDE before storing it in the database, it looks like gibberish.
#realJSOP 22-Apr-19 15:16pm
Show us the string.
Member 14077625 22-Apr-19 15:25pm
I tried using BitConverter as follows:
byte[] theBytesData = BitConverter.GetBytes(stations[0]);
theMemoField = BitConverter.ToString(theBytesData);

But it didn't produce the same string that the C++ code produces.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900