简体   繁体   中英

How can I get the substring of every two characters from text in a textbox with an unknown length

I'm working on a side project that generates code with four bytes of hex code (ex. 12 12 12 12) and I got the part down that generates it but I want to be able to use it this with any length of hex code (ex. 12 12 12 12 12 12 12 12 12 12, basically any length) I don't know how though, here's what I have right now

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;

namespace CodeGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void copyoutputBytesbtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(outputbytesTxtbox.Text);
        }

        private void copyfullCodebtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(fullcodeTxtbox.Text);
        }

        private void inputBytestxtbox_TextChanged(object sender, EventArgs e)
        {
            if (inputBytestxtbox.Text.Contains(" "))
            {
                string inputBytes = inputBytestxtbox.Text.Replace(" ", String.Empty);
                string f1 = inputBytes.Substring(0, 2);
                string f2 = inputBytes.Substring(2, 2);
                string f3 = inputBytes.Substring(4, 2);
                string f4 = inputBytes.Substring(6, 2);
                outputbytesTxtbox.Text = "0x" + f1 + " 0x" + f2 + " 0x" + f3 + " 0x" + f4;
                //original format: 12 12 12 12//
                //output format: 0x12 0x12 0x12 0x12//
            }
        }
    }
}

I want to be able to use any length of hex code not just four, is there anyway I can go about doing this?

You can use for loop:

static void Main(string[] args)
{
    string input = "121212121212121"; // Your input form textblock
    string[] result = SplitByTwo(input); // splitting the string
    foreach (string s in result) // showing results
        Console.Write($"{s} ");
}

public static string[] SplitByTwo(string input)
{
    // the + 1 is here in case that the length is not divisible by 2
    // examle: if length is 6 the array kength must be 3 | (6 + 1) / 2 = 3 (integer division)
    //         if length is 7 the array length must be 4 | (7 + 1) / 2 = 4
    string[] output = new string[(input.Length + 1) / 2];
    for (int i = 0; i < (input.Length + 1) / 2; i++)
    {
        if ((i * 2) + 1 == input.Length) // in case that the last split only contains 1 character
            output[i] = input.Substring(i * 2, 1);
        else
            output[i] = input.Substring(i * 2, 2);
    }
    return output;
}

output:

12 12 12 12 12 12 12 1

EDIT:

If your input has the spaces you can simply split the string and than join it back:

string input = "12 12 12 12 12 12 12 1";
string output = "0x" + string.Join(" 0x", input.Split(' '));
Console.WriteLine(output);

Output:

0x12 0x12 0x12 0x12 0x12 0x12 0x12 0x1

Please, try this code bellow, and give us feedback if it works fine:

private void inputBytestxtbox_TextChanged(object sender, EventArgs e)
{
            if (inputBytestxtbox.Text.Contains(" "))
            {
                const int step = 2;
                string inputBytes = inputBytestxtbox.Text.Replace(" ", String.Empty);
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < inputBytes.Length; i+=step)
                {
                   string fx = inputBytes.Substring(i, step);
                   sb.Append("0x");
                   sb.Append(fx);
                   if(i <= inputBytes.Length - step)
                   {
                      sb.Append(" ");
                   }
                }
                outputbytesTxtbox.Text = sb.ToString();
            }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM