Is there a way to extract a number from an unknown string?
For example if I have a string containing "115ThisIsAStringOfData" I would like to be able to parse out the 1 and 15 from it as an integer.
However I would need to be able to do this in a way where I can parse the two numbers out when its possible for the lead numbers to be written as 1214 for the two integers 12 and 14, or 18 where its the integers 1 and 8.
To answer the question of why I need something like this: I'm reading packets of data from a device over a serialport. The first integer represents the packet number we're on, and the second represents the total number of packets we're getting from the device. Currently, we are expecting to receive 18 packets of data 62 bytes in length per packet at most, but that may change to be significantly more.
Here is the code we use to read data from the serial port:
SerialPort comport = new SerialPort();
int p = comport.BytesToRead;
string currentString = "";
for (int i = 0; i < p; i++)
{
string thisChar = comport.ReadExisting();
currentString += thisChar;
}
I can't change the algorithm above, as we have various types of data we receive from the device that does not require me to know or care about integer values in the string. I have a flag set in another class that I can access and read, that tells me when I will expect a set of packets, versus when i will expect normal data. But the class this code is in is strictly to get data, not organize it or analyze. The variable currentString is passed to another class after to analyze the data and organize.
So does anyone have any ideas? its also possible the data following the total number of packets to contain integers.

New Topic/Question
Reply




MultiQuote






|