Hi
I get data from a serial port in ascii characters. only numeric characters are allowed and I check for illegal characters with regular expressions. This part works fine and if an illegal character presents itself the regular expression method catches it and display them in variable m. then I take the string and split it into the different words wich consists of 4 bytes each. the illegal character normaly sits as byte 1 for example "q123" the problem is that if a word contains a character other than numeric i need to change the 4 bytes to "0" and it need to be "0000" then.
should i change it in the string with string.Replace before I split it into an array or when it's an array and how?

String can't be modified in .net. You could use array of byte or array of char. Ascii code could be presented as byte (8 bits).

byte[] buffer=new byte[4096];

If string length will be short it also quite efficient to use rebuild new string with replace

The problem is the string is very long. If i Use

tempData.replace("q", "0");

the word after being splitted into a array reads "0234" instead of "0000". Only the "q" gets replaced by a zero.
I need to make the whole 4 byte word 0's.
If I use a character array, how would I find out what index numbers in the array has the illegal character and then convert the whole value or word in the specific index to four "0"

You could make an array of 4 byte long arrays... Then you could just replace the 4-byte arrays where position 0 has an invalid character with a buffer full of 0's.

Does that make sense?

Sorry not really.
I have to create an array in an array?
how do I check if there is an invalid character in an array?
I know how to in a string.

So just so we're clear, you have a string, you split it up into chunks of 4 characters each, but it's basically an array of strings?

Why not just loop through the elements, test each one, and for each one that contains an illegal character, simply set the element equal to "0000"? What am I missing?

Sure, there's that whole thing about strings being immutable and you're technically creating a new string, but it seems to be the most expedient method of dealing with it. If it's a resources-critical app, you could explore fixing it beforehand using something like a StringBuilder object, but I'm hardpressed at the moment to come up with the specific algorithm to accomplish it which doesn't also involve the temporary creation of additional strings, thus defeating the purpose.

Edit: On a lark, I came up with this.

string input = "0123q587123487xq9876"; // 0123   q587   1234   87xq   9876 
            Char[] chars = input.ToCharArray ();

            for (int i = 0; i < input.Length; i += 4)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (!Char.IsDigit(chars[i + j]))
                    {
                        chars[i] = '0';
                        chars[i + 1] = '0';
                        chars[i + 2] = '0';
                        chars[i + 3] = '0';
                        break;
                    }
                }
            }

            string fixedInput = new string(chars);
            Console.WriteLine(fixedInput);
            Console.Read();

Don't know if that's what you're looking for, though. But that creates a "fixedInput" that you could then split into your 4-byte chunks.

Edited 6 Years Ago by apegram: n/a

Comments
neatly coded, pretty much what i would have done

Hi Apegram

Thanks
thats basically what needs to happen, the string I receive is comma seperated and looks basically like this 2341,3456,2345,5436.
I then split it in an array of strings, then I convert each string to a double of four digits which I use in my calculations.

I think I will beable to change your code to work.
Thanks alot for the help

How to convert string --> '0123' to integer...
after converting the same '0123' has to be retained while printing it,,but its dispplayinh '123' instead of '0123'

This article has been dead for over six months. Start a new discussion instead.