Optimization Challenge

J

Jeff B.

Hello,

I'm trying to optimize some code for converting a delimited string into an
array of bytes. An example of the string to be converted is:

"2b,2c,2d,2e,2f,30,31,32,33,34,35,36,37,38,39,3a,3b"

Where each element is a hex number, separated by a comma, that should be
converted into a byte array. Here is a code snippet that I quickly put
together and am using currently:

<begin snippet>
string[] numbers = valueLine.Split(',');
byte[] bytes = new byte[numbers.Length];
int index = 0;

foreach(string item in numbers)
{
bytes[index] = byte.Parse(item,
System.Globalization.NumberStyles.HexNumber);
index++;
}
<end snippet>

Does anyone know of a quicker way to get the delimited string into a byte
array? I'm looking for the quickest algorithm and not necessarily the
shortest amount of code.

--- Thanks, Jeff

--

Jeff Bramwell
Digerati Technologies, LLC
www.digeratitech.com

Manage Multiple Network Configurations with Select-a-Net
www.select-a-net.com
 
J

Jon Skeet [C# MVP]

Jeff B. said:
I'm trying to optimize some code for converting a delimited string into an
array of bytes. An example of the string to be converted is:

"2b,2c,2d,2e,2f,30,31,32,33,34,35,36,37,38,39,3a,3b"

Where each element is a hex number, separated by a comma, that should be
converted into a byte array. Here is a code snippet that I quickly put
together and am using currently:

<begin snippet>
string[] numbers = valueLine.Split(',');
byte[] bytes = new byte[numbers.Length];
int index = 0;

foreach(string item in numbers)
{
bytes[index] = byte.Parse(item,
System.Globalization.NumberStyles.HexNumber);
index++;
}
<end snippet>

Does anyone know of a quicker way to get the delimited string into a byte
array? I'm looking for the quickest algorithm and not necessarily the
shortest amount of code.

Here's a pretty quick version. It assumes the commas are all there (and
without any extra data, whitespace etc).

using System;

class Test
{
static void Main()
{
byte[] b = ParseHexBytes
("2b,2c,2d,2e,2f,30,31,32,33,34,35,36,37,38,39,3a,3b");
Console.WriteLine (BitConverter.ToString(b));
}

static readonly byte[] LookupTable;

// Static constructor to set up the lookup table
static Test()
{
LookupTable = new byte[65536];
for (int i=0; i < 65536; i++)
{
LookupTable=255;
}
for (char c = '0'; c <= '9'; c++)
{
LookupTable[c]=(byte)(c-'0');
}
for (char c = 'a'; c <= 'f'; c++)
{
LookupTable[c]=(byte)(c-('a'-10));
}
for (char c = 'A'; c <= 'F'; c++)
{
LookupTable[c]=(byte)(c-('A'-10));
}
}

public static byte[] ParseHexBytes (string data)
{
byte[] ret = new byte[(data.Length+1)/3];

int index=0;
for (int i=0; i < ret.Length; i++)
{
byte v1 = LookupTable[data[index++]];
byte v2 = LookupTable[data[index++]];
// Skip the comma
index++;
if (v1==255 || v2==255)
{
throw new FormatException ("Invalid hex string");
}

ret=(byte)((v1<<4) + v2);
}
return ret;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top