Takes in any string and convert it into a Byte array, suitable for e.g. insertion into a REG_BINARY Registry value.
//-----------------------------------------------------------------------
// <copyright file="Utility.cs" company="ParanoidMike">
// Copyright (c) ParanoidMike. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ParanoidMike
{
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
/// <summary>
/// Reusable functions for many uses.
/// </summary>
public static class Utility
{
/// <summary>
/// Takes in any string and convert it into a Byte array, suitable for e.g. insertion into a REG_BINARY Registry value.
/// </summary>
/// <param name="inputString">
/// String value to be converted to a Byte array.
/// </param>
/// <returns>
/// Byte array, converted from the input String value.
/// </returns>
public static byte[] ConvertStringToByteArray(string inputString)
{
byte[] outputByteArray;
string[] hexCodesFromString = inputString.Split(',');
int upperBound = hexCodesFromString.GetUpperBound(0);
outputByteArray = new byte[upperBound]; // doing this to resolve uninitialized variable error that appeared below
for (int i = 0; i < upperBound; i++)
{
outputByteArray[i] = Convert.ToByte(hexCodesFromString[i], 16);
}
return outputByteArray;
}
}
}
Related examples in the same category