怎么把十六进制的字符串格式化对齐?

yundou 2015-10-28 11:55:39
682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416
这样的16进制字符串怎么格式化对齐,需要这样的
68 24 19 70 06 00 00 68 03 20 32 33 33 3A 33 33 33 。。。。。。。
...全文
319 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forty2 2015-10-28
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
... 目前能想到最快的方法
有时候‘最快’并不总是最佳的选择。还可以考虑代码的可读性,简洁性,可维护性等。 比如,除非有特殊的性能要求,你2楼的代码就可能就是好的选择。
  • 打赏
  • 举报
回复
string txt = @"682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
StringBuilder tmp = new StringBuilder();
char split = ' ';
for (var i = 0; i < txt.Length; i++)
{
    tmp.Append(txt[i]);
    if (i % 2 == 1 && i != txt.Length - 1)
    {
        tmp.Append(split);
    }
}
Console.WriteLine(tmp.ToString());
Poopaye 2015-10-28
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
优化代码的确很重要,但只有 starfd 的写法没有在尾部附加空格
string txt = @"682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
StringBuilder sb = new StringBuilder(txt, 0, 2, (int)(txt.Length * 1.5));
for (int j = 2; j < txt.Length; j += 2)
	sb.Append(' ').Append(txt, j, 2);
string result = sb.ToString();
目前能想到最快的方法
  • 打赏
  • 举报
回复
引用 6 楼 Forty2 的回复:
@starfd 代码复杂是一个原因。 原因二是‘前置匹配’需要回溯,要考虑它的复杂度。 原因三是string[]可能产生大量的 @ajianchina 因为s.Insert的复杂度为O(N) 你代码的整体复杂度为O(N*N)

static void Main(string[] args)
{
    string txt = "419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
    for (int i = 0; i < 8; i++) txt = txt + txt;

    Measure(() =>
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < txt.Length; i++) { if ((i - 1) % 2 == 1)sb.Append(' '); sb.Append(txt[i]); }
        string s2 = sb.ToString();
    });
    Measure(() =>
    {
        string s2 = Regex.Replace(txt, @"\w\w", "$0 ").TrimEnd();
    });

    Measure(() =>
    {
        string s2 = string.Join(" ", Regex.Split(txt, @"(?<=^(?:\w{2})+)"));
    });
    Measure(() => 
    {
        for (int i = 2; i < txt.Length; i += 3)
        {
            txt = txt.Insert(i, " ");
        }
    });
    // 运行结果
    // Elapsed: 0ms
    // Elapsed: 8ms
    // Elapsed: 19597ms
    // Elapsed: 81ms
}
static void Measure(Action action)
{
    Stopwatch sw = Stopwatch.StartNew();
    action();
    Console.WriteLine("Elapsed: {0}ms", sw.ElapsedMilliseconds);
}
回溯问题可以通过固化组解决,即 将正则修正
@"(?<=^(?>(?:\w{2})+))"
xuzuning 2015-10-28
  • 打赏
  • 举报
回复
优化代码的确很重要,但只有 starfd 的写法没有在尾部附加空格
Forty2 2015-10-28
  • 打赏
  • 举报
回复
@starfd 代码复杂是一个原因。 原因二是‘前置匹配’需要回溯,要考虑它的复杂度。 原因三是string[]可能产生大量的 @ajianchina 因为s.Insert的复杂度为O(N) 你代码的整体复杂度为O(N*N)

static void Main(string[] args)
{
    string txt = "419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
    for (int i = 0; i < 8; i++) txt = txt + txt;

    Measure(() =>
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < txt.Length; i++) { if ((i - 1) % 2 == 1)sb.Append(' '); sb.Append(txt[i]); }
        string s2 = sb.ToString();
    });
    Measure(() =>
    {
        string s2 = Regex.Replace(txt, @"\w\w", "$0 ").TrimEnd();
    });

    Measure(() =>
    {
        string s2 = string.Join(" ", Regex.Split(txt, @"(?<=^(?:\w{2})+)"));
    });
    Measure(() => 
    {
        for (int i = 2; i < txt.Length; i += 3)
        {
            txt = txt.Insert(i, " ");
        }
    });
    // 运行结果
    // Elapsed: 0ms
    // Elapsed: 8ms
    // Elapsed: 19597ms
    // Elapsed: 81ms
}
static void Measure(Action action)
{
    Stopwatch sw = Stopwatch.StartNew();
    action();
    Console.WriteLine("Elapsed: {0}ms", sw.ElapsedMilliseconds);
}
ajianchina 2015-10-28
  • 打赏
  • 举报
回复

string s = "682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
for (int i = 2; i < s.Length; i +=3)
{
	s = s.Insert(i, " ");
}
  • 打赏
  • 举报
回复
引用 3 楼 Forty2 的回复:
[quote=引用 1 楼 starfd 的回复:] txt = string.Join(" ", Regex.Split(txt, @"(?<=^(?:\w{2})+)"));
@starfd,建议不要给出这样的代码。或者至少要备注它的短处。[/quote] 好吧,其实用Matches或者Replace更简单,我这个反而弄复杂了
Forty2 2015-10-28
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
txt = string.Join(" ", Regex.Split(txt, @"(?<=^(?:\w{2})+)"));
@starfd,建议不要给出这样的代码。或者至少要备注它的短处。
Poopaye 2015-10-28
  • 打赏
  • 举报
回复
Regex.Replace("682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416", @"\w\w", "$0 ").TrimEnd()
  • 打赏
  • 举报
回复
string txt = @"682419700600006803203233333A333333333EF7D8808AB24355F4C9F8A9FD8C4796574CA33933333333B416";
txt = string.Join(" ", Regex.Split(txt, @"(?<=^(?:\w{2})+)"));
Console.WriteLine(txt);
crystal_lz 2015-10-28
  • 打赏
  • 举报
回复
在一个一瞬间就能运行完成的代码上 纠结效率 我也是醉了 楼主又没说这是一个很长很长的文本。。。。
ajianchina 2015-10-28
  • 打赏
  • 举报
回复
且论第一把给出的代码,还是比你快 楼主未曾对此提出性能要求,我只是写个浅显易懂的而已。
  • 打赏
  • 举报
回复
引用 12 楼 ajianchina 的回复:
怎么听见有人说我的代码也是属于最后附上空格的,呵呵,且不论是否最快,我的代码永远不会在两头产生空格的,我首先会考虑这个问题。
但会每次循环都产生一个新的字符串 虽然我接了这段话,但不是我说的会产生空格
ajianchina 2015-10-28
  • 打赏
  • 举报
回复
怎么听见有人说我的代码也是属于最后附上空格的,呵呵,且不论是否最快,我的代码永远不会在两头产生空格的,我首先会考虑这个问题。

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧