Home > Software design >  Convert string to array of Uint16
Convert string to array of Uint16

Time:01-06

happy new year !

I have collected data from an OPC UA server, the data is :

0;0;0;0;0;

I need to write this data to my OPC UA server, the good syntax need to be :

UInt16[] temp = { 0, 0, 0, 0, 0 };

How can I convert the first string to have my array of Uint16 ?

CodePudding user response:

Using linq :

var dataString = "0;0;0;0;0;";
var temp = dataString.Split(';')
           .Where(s => !s.Equals(string.Empty))
           .Select(value => Convert.ToUInt16(value))
           .ToArray();

enter image description here

CodePudding user response:

assuming

var s = "0;0;0;0;0;";

try this

UInt16[] temp = s.Split( ";" , StringSplitOptions.RemoveEmptyEntries)
.Select(p => Convert.ToUInt16(p)).ToArray();

CodePudding user response:

thanks all, Finally it's working with this :

string data = childNode.InnerXml; //0;0;0;0;0;

string[] words = data.Split(';').SkipLast(1).ToArray(); //need to remove the last empty element of the array
                                    
UInt16[] myInts = Array.ConvertAll(words, s => UInt16.Parse(s));
  •  Tags:  
  • Related