You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多数组元素组合生成十六进制字符串及文本匹配替换需求

从多组byte数组生成递增8位十六进制串并替换文本的解决方案

一、生成无去重的递增8位十六进制字符串

既然Union会自动去重不符合需求,直接用嵌套循环遍历4组数组的元素,按顺序拼接成8位十六进制串即可。以C#为例(结合你提到的BitConverter,推测是C#环境):

// 假设你已经准备好4组byte数组,对应8位十六进制的四个字节段
byte[] group1 = new byte[] { 0x00, 0x01, 0x02 }; // 对应前两位十六进制
byte[] group2 = new byte[] { 0x00 }; // 中间两位
byte[] group3 = new byte[] { 0x00 }; // 倒数两位
byte[] group4 = new byte[] { 0x00 }; // 最后两位

List<string> hexStrings = new List<string>();

// 嵌套循环遍历所有组合,顺序生成8位十六进制串
foreach (byte b1 in group1)
{
    foreach (byte b2 in group2)
    {
        foreach (byte b3 in group3)
        {
            foreach (byte b4 in group4)
            {
                // 每个byte转两位十六进制,拼接成8位字符串
                string hexStr = $"{b1:X2}{b2:X2}{b3:X2}{b4:X2}";
                hexStrings.Add(hexStr);
            }
        }
    }
}

这样生成的列表会严格按你遍历数组的顺序产出十六进制串,不会丢失任何组合。

二、替换文本为递增编号的“Record {0}”

拿到生成的十六进制串列表后,直接遍历替换即可。如果要按顺序给每个串分配递增编号:

string targetText = "你的多行文本框内容";
int recordId = 1;

// 逐个替换每个十六进制串
foreach (string hex in hexStrings)
{
    targetText = targetText.Replace(hex, $"Record {recordId}");
    recordId++;
}

// 把替换后的内容放回文本框
yourMultiLineTextBox.Text = targetText;

如果文本里有重复的十六进制串,上面的代码会替换所有匹配项。要是需要按出现顺序逐个替换(每个串只替换一次),可以用正则表达式的MatchEvaluator:

int counter = 1;
string replacedText = Regex.Replace(targetText, 
    string.Join("|", hexStrings.Select(s => Regex.Escape(s))),
    match => $"Record {counter++}");

内容的提问来源于stack exchange,提问作者Phil G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:50:24