Author Topic: convert string to float/double array  (Read 662 times)

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
convert string to float/double array
« on: October 29, 2018, 08:19:16 PM »
I have some serial data string that have format like the s string below. Using some AnalogValueDisplay meters and some vb code, I was able to display them properly
Code: [Select]
Dim s As String = "0.5969675993919372, 2268826624, 1.391914329528809, 654071936" + vbCrLf
        s = s.Replace(vbCrLf, String.Empty)
        Dim sa() As String = s.Split(","c)
        AnalogValueDisplay1.Value = Format(CDbl(sa(0)), "0.000")
        AnalogValueDisplay2.Value = Format(CDbl(sa(1)) / 1000000, "0000")
        AnalogValueDisplay3.Value = Format(CDbl(sa(2)), "0.000")
        AnalogValueDisplay4.Value = Format(CDbl(sa(3)) / 1000000, "0000")
But, C# side have some cool way to convert it directly to float array, I tried to convert to vb format but keep getting error for it:

Code: [Select]
double[] doubles = sarray.Split(',').Select(Double.Parse).ToArray();

Array.ConvertAll(sarray.Split(','), Double.Parse);
double[] doubles = Array.ConvertAll(sarray.split(','), new Converter<string, double>(Double.Parse));

Thanks in advance.
« Last Edit: October 29, 2018, 09:17:12 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: convert string to float / double array
« Reply #1 on: October 29, 2018, 08:26:40 PM »
This seemed to work:
Code: [Select]
       Dim doubleAry As Double() = Array.ConvertAll(s.Split(","c), New Converter(Of String, Double)(AddressOf Double.Parse))
        AnalogValueDisplay1.Value = Format(doubleAry(0), "0.000")
        AnalogValueDisplay2.Value = Format(doubleAry(1) / 1000000, "0000")
        AnalogValueDisplay3.Value = Format(doubleAry(2), "0.000")
        AnalogValueDisplay4.Value = Format(doubleAry(3) / 1000000, "0000")
« Last Edit: October 29, 2018, 09:17:30 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================