为字母或数字添加0x

时间:2014-09-23 17:20:53

标签: c#

我想在每两个字母或数字中添加0x;例如:

50573953463435464438414B58413135

我希望它像:

0x50, 0x57, 0x39, 0x53, 0x46, 0x34 ,0x35, 0x46, 0x44, 0x38, 0x41, 0x4b, 0x58, 0x41, 0x31, 0x35

之后,我想将它添加到这样的字节:

byte[] key = new byte[] { 0x50, 0x57, 0x39, 0x53, 0x46, 0x34 ,0x35, 0x46, 0x44, 0x38, 0x41, 0x4b, 0x58, 0x41, 0x31, 0x35 };

5 个答案:

答案 0 :(得分:1)

如果您尝试从十六进制数字字符串创建格式化字符串:

string hexString = "50573953463435464438414B58413135";
string formattedHexString = string.Join("", Enumerable.Range(0, hexString.Length)
            .Where(z => z%2 == 0)
            .Select(z => "0x" + hexString.Substring(z, 2) + ", ")
            .ToArray()).Trim().TrimEnd(',');
Console.WriteLine(formattedHexString);

// Output:
// 0x50, 0x57, 0x39, 0x53, 0x46, 0x34, 0x35, 0x46, 0x44, 0x38, 0x41, 0x4B, 0x58, 0x41, 0x31, 0x35

如果您尝试从十六进制字符串创建字节数组。看看this SO question

答案 1 :(得分:0)

这是你要求的吗?

var str = "byte[] key = new byte[] { " + 
   Regex.Replace("50573953463435464438414B58413135", @"[0-9A-F]{2}", "0x$0, ") +
   "};";

str的值是

byte[] key = new byte[] { 0x50, 0x57, 0x39, 0x53, 0x46, 0x34, 0x35, 0x46, 0x44, 0x38, 0x41, 4B0x58, 0x41, 0x31, 0x35, };

答案 2 :(得分:0)

var txt = "50573953463435464438414B58413135";
var split = 2;
var q=  Enumerable.Range(0, txt.Length / split)
        .Select(i => string.Format("0x{0}", txt.Substring(i * split, split)));
        q.Dump();

在LinqPad中,结合了类似的答案

<强> 修改

获取字节数组修改上面的内容包括:

var txt = "50573953463435464438414B58413135";
var split = 2;
var q=  Enumerable.Range(0, txt.Length / split)
        .Select(i => string.Format("0x{0}", txt.Substring(i * split, split)))
        .Select (x => Convert.ToByte(x, 16)); // Add
        q.Dump();

答案 3 :(得分:0)

完成测试程序,虽然我已经看到了一些强大的(几乎)oneliners:

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Hexify
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string pattern = "[0-9A-F][0-9A-F]";
            string input = "50573953463435464438414B58413135";

            var matchCollection = Regex.Matches(input, pattern);

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

            foreach (Match m in matchCollection)
            {
                strings.Add(m.Value);
            }

            string result = String.Empty;

            if (strings.Count > 0)
            {
                result = "0x" + String.Join(", 0x", strings);
            }

            Assert.AreEqual("0x50, 0x57, 0x39, 0x53, 0x46, 0x34, 0x35, 0x46, 0x44, 0x38, 0x41, 0x4B, 0x58, 0x41, 0x31, 0x35", result);
        }
    }
}

答案 4 :(得分:0)

您是否希望将字符串从其十六进制对转换为等效字节,是否正确?

 List<byte> byteValues = new List<byte>();
 string split = "50573953463435464438414B58413135";

 for (int idx = 0; idx < split.Length - 1; idx += 2)
 {
     var value = split.Substring(idx, 2);

     byteValues.Add(Convert.ToByte(value, 16));
  }

  foreach (var b in byteValues)
       Console.WriteLine(b);