-3

I want to add 0x to each two letters or numbers; for example:

50573953463435464438414B58413135

I want it to be like:

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

and after that, I want to add it to byte like this:

byte[] key = new byte[] { 0x50, 0x57, 0x39, 0x53, 0x46, 0x34 ,0x35, 0x46, 0x44, 0x38, 0x41, 0x4b, 0x58, 0x41, 0x31, 0x35 };
Bob2Chiv
  • 1,858
  • 2
  • 19
  • 29

5 Answers5

1

Is this what you're asking for?

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

str's value is

byte[] key = new byte[] { 0x50, 0x57, 0x39, 0x53, 0x46, 0x34, 0x35, 0x46, 0x44, 0x38, 0x41, 0x4B, 0x58, 0x41, 0x31, 0x35, };
SSpoke
  • 5,656
  • 10
  • 72
  • 124
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • i use you code like that : var str = Regex.Replace(result, @"[0-9A-F]{2}", "0x$0, "); and i do like that byte[] key = new byte[] { str }; i got error @tim-s – abdulaziz mohammed Sep 23 '14 at 18:01
1

If you are trying to create a formatted string from a string of hex numbers:

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

If you are trying to create a byte array from a hex string. look at this SO question.

Community
  • 1
  • 1
Icemanind
  • 47,519
  • 50
  • 171
  • 296
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();

In LinqPad, combining a similar answer

EDIT

to get the byte array modify the above to include:

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();
reckface
  • 5,678
  • 4
  • 36
  • 62
0

Complete test program, though I already saw some powerful (almost) 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);
        }
    }
}
Pieter21
  • 1,765
  • 1
  • 10
  • 22
0

You're looking to convert the string from it's hexadecimal pairs to the byte equivalents, correct?

 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);
tbddeveloper
  • 2,407
  • 1
  • 23
  • 39