简体   繁体   English

如何从长度未知的文本框中的文本中获取每两个字符的 substring

[英]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我正在开发一个使用四个字节的十六进制代码(例如 12 12 12 12)生成代码的辅助项目,我得到了生成它的部分,但我希望能够将它与任何长度的十六进制代码一起使用(例如 12 12 12 12 12 12 12 12 12 12,基本上任何长度)我不知道如何,这就是我现在所拥有的

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?我希望能够使用任何长度的十六进制代码而不仅仅是四个,无论如何我可以 go 这样做吗?

You can use for loop:您可以使用for循环:

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: 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: 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();
            }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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