Optimization Challenge

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff B.

    #1

    Optimization Challenge

    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.Globaliz ation.NumberSty les.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


    Manage Multiple Network Configurations with Select-a-Net



  • Jon Skeet [C# MVP]

    #2
    Re: Optimization Challenge

    Jeff B. <nobody@nowhere .com> wrote:[color=blue]
    > 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.Globaliz ation.NumberSty les.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.[/color]

    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,2 f,30,31,32,33,3 4,35,36,37,38,3 9,3a,3b");
    Console.WriteLi ne (BitConverter.T oString(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[i]=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[i]=(byte)((v1<<4) + v2);
    }
    return ret;
    }
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...