LINQ in Action - LINQ Book & News

Hex Dump using LINQ (in 7 lines of code)

Eric White has posted an interesting LINQ query on his blog that shows how to create a Hex Dump in something like 7 lines of code.

Of course, this is not production grade code, but it's another good example that demonstrates the expressiveness of LINQ.

Here is the code:

byte[] ba = File.ReadAllBytes("test.xml");
int bytesPerLine = 16;
string hexDump = ba.Select((c, i) => new { Char = c, Chunk = i / bytesPerLine })
    .GroupBy(c => c.Chunk)
    .Select(g => g.Select(c => String.Format("{0:X2} ", c.Char))
        .Aggregate((s, i) => s + i))
    .Select((s, i) => String.Format("{0:d6}: {1}", i * bytesPerLine, s))
    .Aggregate("", (s, i) => s + i + Environment.NewLine);
Console.WriteLine(hexDump);

Here is a sample output:

000000: FF FE 3C 00 3F 00 78 00 6D 00 6C 00 20 00 76 00
000016: 65 00 72 00 73 00 69 00 6F 00 6E 00 3D 00 22 00
000032: 31 00 2E 00 30 00 22 00 20 00 65 00 6E 00 63 00
000048: 6F 00 64 00 69 00 6E 00 67 00 3D 00 22 00 75 00
000064: 3E 00

Eric White reports that he typically notices that declarative code is only 20% as long as imperative code.

Published Friday, March 12, 2010 10:34 PM by Fabrice Marguerie

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

GooBoy said:

What an unreadable pile of goo...

March 15, 2010 6:12 PM
 

Hmm said:

Goo? Nah, it's primordial soup! (hasn't yet evolved to goo)

May 25, 2010 4:07 PM
 

ptr_vo1d said:

Interesting example, but not adaptive to the real world, where you need maintainability and performance.

This code has nearly the same size but is readable and 4-5 times faster at runtime:

private static string HexDump(byte[] rawData)

       {

           StringBuilder hexDump = new StringBuilder();

           for(int i=0; i < rawData.Length; i += 16)

           {

               hexDump.AppendFormat("{0:d6}: ", i);

               for(int j=0; j < 16; j++)

                   hexDump.AppendFormat("{0:X2} ", rawData[i+j]);

               hexDump.AppendFormat("{0}", Environment.NewLine);

           }

           return hexDump.ToString();

       }

November 23, 2011 10:09 AM
 

ptom said:

I tried the LINQ code on a 1M file and waited...patiently...had a cup of coffee, and waited...patiently...took out the trash and fed the dogs, and waited...patiently...finally, I killed the process. I tried again on a 1k file and it came back in about 1 minute, so the LINQ code definitely works.

I ran my existing hexdump code on the 1M file and it  came back in seconds. I ran the code posted by ptr_vo1d and it came back in seconds.

My question to Fabrice and Eric is: how do you make Eric's code production grade? I guessing it will take at least 20 lines of code and still run twice as long as non-LINQ. I'm a fan of the concept of LINQ, but they really have to find a way to optimize it.

March 14, 2012 10:42 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit