0

How would you convert a parapraph to hex notation, and then back again into its original string form?

(C#)

A side note: would putting the string into hex format shrink it the most w/o getting into hardcore shrinking algo's?

  • Are you talking about base64? I was going to post an answer, but on re-reading I'm not sure you know what you mean. Can you give an example of a string you want to convert, and then an example of the same string converted? – Adam Davis Oct 20 '08 at 19:47
  • Agreed with Adam -- I'm not sure the poster knows what he wants. If you want to turn "Hello" into "48656C6C6F", that's pretty easy, but I don't think that's quite it. – Coderer Oct 20 '08 at 20:26
  • I'm guessing homework assignment. – Bill K Oct 20 '08 at 21:17

5 Answers5

6

What exactly do you mean by "hex notation"? That usually refers to encoding binary data, not text. You'd need to encode the text somehow (e.g. using UTF-8) and then encode the binary data as text by converting each byte to a pair of characters.

using System;
using System.Text;

public class Hex
{
    static void Main()
    {
        string original = "The quick brown fox jumps over the lazy dog.";

        byte[] binary = Encoding.UTF8.GetBytes(original);
        string hex = BytesToHex(binary);
        Console.WriteLine("Hex: {0}", hex);
        byte[] backToBinary = HexToBytes(hex);

        string restored = Encoding.UTF8.GetString(backToBinary);
        Console.WriteLine("Restored: {0}", restored);
    }

    private static readonly char[] HexChars = "0123456789ABCDEF".ToCharArray();

    public static string BytesToHex(byte[] data)
    {
        StringBuilder builder = new StringBuilder(data.Length*2);
        foreach(byte b in data)
        {
            builder.Append(HexChars[b >> 4]);
            builder.Append(HexChars[b & 0xf]);
        }
        return builder.ToString();
    }

    public static byte[] HexToBytes(string text)
    {
        if ((text.Length & 1) != 0)
        {
            throw new ArgumentException("Invalid hex: odd length");
        }
        byte[] ret = new byte[text.Length/2];
        for (int i=0; i < text.Length; i += 2)
        {
            ret[i/2] = (byte)(ParseNybble(text[i]) << 4 | ParseNybble(text[i+1]));
        }
        return ret;
    }

    private static int ParseNybble(char c)
    {
        if (c >= '0' && c <= '9')
        {
            return c-'0';
        }
        if (c >= 'A' && c <= 'F')
        {
            return c-'A'+10;
        }
        if (c >= 'a' && c <= 'f')
        {
            return c-'A'+10;
        }
        throw new ArgumentOutOfRangeException("Invalid hex digit: " + c);
    }
}

No, doing this would not shrink it at all. Quite the reverse - you'd end up with a lot more text! However, you could compress the binary form. In terms of representing arbitrary binary data as text, Base64 is more efficient than plain hex. Use Convert.ToBase64String and Convert.FromBase64String for the conversions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • amazing! What does b >> 4 do? shifting? –  Oct 20 '08 at 19:52
  • Yes, it shifts the byte right, so you end up with a single nybble (0-15). – Jon Skeet Oct 20 '08 at 19:57
  • visually speaking, how does that look? isn't the value of the byte changing now? –  Oct 20 '08 at 20:07
  • The byte itself isn't changing - numbers don't actually change their values. It's computing the value of the byte shifted right. To put it another way, just think of that expression as "b/16". – Jon Skeet Oct 20 '08 at 20:17
  • Thanks Jon, this has helped me a lot too. I've got to process some text that has been stored in a varchar field as Hex for some reason. The weird things we encounter in day to day development, lol :-) – Doctor Jones Jul 09 '09 at 08:44
1
public string ConvertToHex(string asciiString)
{
    string hex = "";
    foreach (char c in asciiString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
0
static byte[] HexToBinary(string s) {
  byte[] b = new byte[s.Length / 2];
  for (int i = 0; i < b.Length; i++)
    b[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
  return b;
}
static string BinaryToHex(byte[] b) {
  StringBuilder sb = new StringBuilder(b.Length * 2);
  for (int i = 0; i < b.Length; i++)
    sb.Append(Convert.ToString(256 + b[i], 16).Substring(1, 2));
  return sb.ToString();
}
Hafthor
  • 16,358
  • 9
  • 56
  • 65
0

While I can't help much on the C# implementation, I would highly recommend LZW as a simple-to-implement data compression algorithm for you to use.

coppro
  • 14,338
  • 5
  • 58
  • 73
0

Perhaps the answer can be more quickly reached if we ask: what are you really trying to do? Converting an ordinary string to a string of a hex representation seems like the wrong approach to anything, unless you are making a hexidecimal/encoding tutorial for the web.

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129