nagaraja
nagaraja

Reputation: 51

How to convert the string with Byte to byte array in c#

I have a string with byte format in it as below

var string="22-74-68-64-62-32-75-74-71-53-5A-6D-44-32-65-61-38-39-43-6A-39-4A-41-3D-3D-22"

Convert this to byte array as

byte[] arr=[22-74-68-64-62-32-75-74-71-53-5A-6D-44-32-65-61-38-39-43-6A-39-4A-41-3D-3D-22]

Upvotes: 1

Views: 1371

Answers (3)

TheGeneral
TheGeneral

Reputation: 81573

Slightly more succinct with Convert.ToByte

var bytes = input.Split('-')
                 .Select(x => Convert.ToByte(x,16))
                 .ToArray();

Additional resources

ToByte(String, Int32)

Converts the string representation of a number in a specified base to an equivalent 8-bit unsigned integer.

Upvotes: 3

JohanP
JohanP

Reputation: 5472

You can use Linq to get it quite tidy

byte[] arr = input.Split('-').Select(i => byte.Parse(i, System.Globalization.NumberStyles.HexNumber)).ToArray();

Upvotes: 2

Prime
Prime

Reputation: 2482

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string input = "22-74-68-64-62-32-75-74-71-53-5A-6D-44-32-65-61-38-39-43-6A-39-4A-41-3D-3D-22";

        //Split string by '-'
        string[] spl = input.Split('-');

        //Parse bytes and add them to a list
        List<byte> buf = new List<byte>();
        foreach(string s in spl) {
            buf.Add(byte.Parse(s, System.Globalization.NumberStyles.HexNumber));
        }

        //Convert list to byte[]
        byte[] bytes = buf.ToArray();

        //Print byte[] into console
        foreach(byte b in bytes)
            Console.WriteLine(b.ToString("X2"));
    }
}

DotNetFiddle

The above code will cause an exception if the input is not a perfect string of bytes delimited by dashes. If you're not expecting a perfect input you'll have to use byte.TryParse like so:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string input = "22-74-68-64-62-32-75-74-71-53-5A-XX-wrgererh-6D-44-32-65-61-38-39-43-6A-39-4A-41-3D-3D-22";

        //Split string by '-'
        string[] spl = input.Split('-');

        //Parse bytes and add them to a list
        List<byte> buf = new List<byte>();
        byte tb;
        foreach(string s in spl) {
            if(byte.TryParse(s, System.Globalization.NumberStyles.HexNumber, null, out tb))
                buf.Add(tb);
        }

        //Convert list to byte[]
        byte[] bytes = buf.ToArray();

        //Print byte[] into console
        foreach(byte b in bytes)
            Console.WriteLine(b.ToString("X2"));
    }
}

DotNetFiddle

Upvotes: 2

Related Questions