Answered Break string and store it into an array

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
Hi guys
I have a simple question. In the following picture there is one Text box which contains numbers as well as alphabets.
I want to break this string and store each letter or number in array. How to do that?
e.g string in text box is 23A45E890, i want it like that array[0] = 2, array[1] = 3, array[2] = A .... so on
1.png


Thanks and best regards
 
The String class has a ToCharArray method, so you simply call that on your String object. That said, you may not even have to do that, depending on how you want to use the data. A String object is an IEnumerable<Char>, which means that you can run a foreach loop over a String to access each Char in turn. If you want random access then creating an array is the way to go.
 
You also can access the individual characters of a string using its indexer.

 
You also can access the individual characters of a string using its indexer.

You can too. I forgot about that for some reason. Calling ToCharArray allows you to set characters as well get them but, if you only want to get them, using the indexer directly would presumably be more efficient.
 
As similarly pointed out, the easiest and most efficient way would be to use each index of the strings position like :
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            string value = "23A45E890";
            for (int i = 0; i < value.Length; i++)
                Debug.WriteLine(value[i]);
        }
This will print out each individual char. Or you could be more elaborate if you're bored due to all this irrelevant lockdown nonsense.

C#:
        internal char[] Char_Arr = { };
        private void button1_Click(object sender, EventArgs e)
        {
            Char_Arr = StrToArray("23A45E890");
        }
        internal char[] StrToArray(string value)
        {
            return value.ToArray();
        }
You don't even need the function, but for example sake... Simply calling ToArray() on your string will give you what you need. Add this to your button and you can see what is output after the returned result :
C#:
            foreach (char c in Char_Arr)
                Debug.WriteLine(c);
If you set a break point on the Char_Arr you can see its contents when you hover over it. Debugging tutorial in my signature should you need it. Screenshot :

Screenshot_16.jpg
 
Hi Guys
Thanks for your replies. Actually i am confused how to do that?
My main Task is to get input from Textbox (i-e 2345J4567AS09843) then seperate each alphabet, convert it into decimal. After this calculate CRC bytes and then show the complete array on another text box

for your understanding see the following code what i have done upto and what i need to do

C#:
private void showSerialNo_Click(object sender, EventArgs e)
        {
            // from frame[0] to frame[5] data is fixed. Not entered by user
            byte slaveAddress = 1;
            byte functionCode = 16;
            ushort startAddress = 32768;
            ushort numberOfPoints = 10;

            byte[] frame = new byte[29];
            frame[0] = slaveAddress;
            frame[1] = functionCode;
            frame[2] = (byte)(startAddress >> 8);
            frame[3] = (byte)startAddress;
            frame[4] = (byte)(numberOfPoints >> 8);
            frame[5] = (byte)numberOfPoints;

            //frame[6] to frame[23] is the data entered by user in text box. which should be converterd to dec from ascii char
            frame[6] = 20;
            frame[7] = 50;
            frame[8] = 49;
            frame[9] = 48;
            frame[10] = 51;
            frame[11] = 48;
            frame[12] = 74;
            frame[13] = 49;
            frame[14] = 57;
            frame[15] = 49;
            frame[16] = 52;
            frame[17] = 69;
            frame[18] = 78;
            frame[19] = 48;
            frame[20] = 51;
            frame[21] = 57;
            frame[22] = 53;
            frame[23] = 55;

            //frame[24] to frame[26] these are always 0
            frame[24] = 0;
            frame[25] = 0;
            frame[26] = 0;

            // frame[27] to frame[28] are the result of CRC Sum
            byte[] checkSum = CRC16(frame);
            frame[27] = checkSum[0];
            frame[28] = checkSum[1];

            // currently data is displaying on console window. but i want to display it on another textbox.
            foreach (var item in frame)
            {
                Console.Write(string.Format("{0:X2} ", item));
            }
            Console.ReadKey();
        }

code for CRC calculation is
C#:
 private static byte[] CRC16(byte[] data) // Method to Calculate CRC Bytes
        {
            byte[] checkSum = new byte[2];
            ushort reg_crc = 0XFFFF;
            for (int i = 0; i < data.Length - 2; i++)
            {
                reg_crc ^= data[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((reg_crc & 0x01) == 1)
                    {
                        reg_crc = (ushort)((reg_crc >> 1) ^ 0xA001);
                    }
                    else
                    {
                        reg_crc = (ushort)(reg_crc >> 1);
                    }
                }
            }
            checkSum[1] = (byte)((reg_crc >> 8) & 0XFF);
            checkSum[0] = (byte)(reg_crc & 0xFF);
            return checkSum;
        }

result of this looks like that, i am getting result on console window but need this on textbox

result.png
 
Last edited:
Break down the problem into smaller problems. Your first problem is getting each character out of the string. What have you done so far? You were shown multiple approaches above. Which one did you pick? What happened when you tried it?

Once you have all the characters, they will already naturally give you back their ASCII values (as shown by @Sheepings' screenshot in post #5). All you need to do is apply the CRC algorithm on that data.

Outputting the contents of your array is simply a matter of building a string and then setting the string in the text box. The best way to do this is to use a StringBuilder, but you could just keeping concatenating to a string.
 
My main Task is to get input from Textbox (i-e 2345J4567AS09843) then seperate each alphabet, convert it into decimal.
Regarding this. What is so different from string value = "23A45E890"; being equal to the text property of a control?
I also don't see any attempt to implement any of the examples you've already received.

Now regarding your new questions. You started off asking one thing, and now you are asking to do three other things which is completely different. Please open new topics for each new question you have. Such as how to convert to double, although a quick search of this forum turns up numerous answers. And also converting bytes or working with bytes is not related to the question you started off asking about. If the code is the same for all three questions, then you may link to your other posts for references. We do it like this so not to confuse topics where its one question per topic.
 
Back
Top Bottom