473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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,3 b"

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
Jul 21 '05 #1
1 914
Jeff B. <no****@nowhere.com> wrote:
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,3 b"

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[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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frank Buss | last post by:
A new challenge: http://www.frank-buss.de/marsrescue/index.html Have fun! Now you can win real prices. -- Frank Buß, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
0
by: Richard Jones | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
0
by: richard | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
8
by: aloha.kakuikanu | last post by:
create table T ( x integer, y integer, ); create index Ti on T(x,y); insert into t select i/1000 as x, mod(i,1000) as y from Integers where i < 1000000
3
by: Thierry | last post by:
For those interested in <b>programming riddles</b>, I would like to announce a new programming challenge I'm just launching at http://software.challenge.googlepages.com This challenge is in its...
0
by: Richard Jones | last post by:
The fifth PyWeek is only a month away. Come along and join the fun: write a video game in a week! There's some really interesting new libraries that have popped up recently. Have a gander on the...
80
by: jacob navia | last post by:
Several people in this group argue that standard C is not portable since there are no compilers for it, etc. I propose this program in Standard C, that I have compiled in several OSes to test if...
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.