# Extracting Integers from String Involving Serial Ports

Page 1 of 1

## 5 Replies - 1784 Views - Last Post: 13 October 2012 - 01:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295479&amp;s=eea9177db115400b1c60d5e53bc220c5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CMDDrake

Reputation: 0
• Posts: 6
• Joined: 08-June 10

# Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 11:14 AM

Greetings all,

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();
string currentString = "";
for (int i = 0; i < p; i++)
{
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.

Is This A Good Question/Topic? 0

## Replies To: Extracting Integers from String Involving Serial Ports

### #2 tlhIn`toq

Reputation: 5839
• Posts: 12,692
• Joined: 02-June 10

## Re: Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 01:04 PM

Once you have the string then you can parse it.
Loop through character by character
Try to convert it to a number
If it can - add it to another string
if it can't - then stop
So you are building up your new string to parse
```1
11
115
115T  <--- Stop looping and keep just 115
```

Now that you have 115 you can .Substring it how ever you need to, then convert those substrings to numbers.

### #3 tlhIn`toq

Reputation: 5839
• Posts: 12,692
• Joined: 02-June 10

## Re: Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 01:11 PM

By the way, you say you can't change this algorythm, but its not a good one.

```SerialPort comport = new SerialPort();
string currentString = "";
for (int i = 0; i < p; i++)
{
currentString += thisChar;
}

```

.ReadExisting should provide you the entire COM port buffer - so you shouldn't have to read the entire buffer over and over in a loop. All that is really happening is you are reading in the entire long string the first time (full string not 1 char). Then for the rest of the loop there is nothing there to read so you keep adding {nothing} to the full string you already read. The loop is just spinning its wheels for no reason.

Not to mention there is no error recovery. This code shouldn't even work at all since you never set the port to COM4 or COM1.

Serial port communication

### #4 CMDDrake

Reputation: 0
• Posts: 6
• Joined: 08-June 10

## Re: Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 01:26 PM

We actually set the port earlier.

Line 1 of the code i gave you is a declaration we have somewhere earlier in code. We set port, baud rate, parity, everything, then read in data

Thanks for the heads up on the loop by the way.

```                int p = comport.BytesToRead;
string currentString = "";

```

would this work better?

Also about the algorithm you propose for the ints, i'm going to have to flow chart something out using the idea of attempt to convert a char in the string to an int and see if its a valid int or not.

This post has been edited by tlhIn`toq: 13 October 2012 - 01:49 PM
Reason for edit:: No need to quote the entire previous message - we all see it

### #5 CMDDrake

Reputation: 0
• Posts: 6
• Joined: 08-June 10

## Re: Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 01:33 PM

```               string currentString = "";

```

i mean this*

### #6 tlhIn`toq

Reputation: 5839
• Posts: 12,692
• Joined: 02-June 10

## Re: Extracting Integers from String Involving Serial Ports

Posted 13 October 2012 - 01:54 PM

CMDDrake, on 13 October 2012 - 02:26 PM, said:

We actually set the port earlier.

That's meaningless. Quite clearly on line one you are making a whole new serialport object. then you start using it immediately without setting ANYTHING about it.

Quote

```1  SerialPort comport = new SerialPort();

```

Anything you are doing with a different SerialPort object that has the same name, but is defined in a different scope has nothing to do with this SerialPort object.

CMDDrake, on 13 October 2012 - 02:26 PM, said:

We actually set the port earlier.
Line 1 of the code i gave you is a declaration we have somewhere earlier in code.

Oh - you want to use to help you with your code - but you aren't actually providing us with the code you are using. Yeah - that's helpful. Best of luck with your project. <unsubscribing from thread>

This post has been edited by tlhIn`toq: 13 October 2012 - 01:55 PM