将段落转换为十六进制常规,然后返回到字符串

时间:2022-02-09 15:48:25

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

你如何将parapraph转换为十六进制表示法,然后再将其转换回原始的字符串形式?

(C#)

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

一个侧面说明:将字符串放入十六进制格式会缩小它进入硬核缩小算法的最多时间吗?

5 个解决方案

#1


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.

“十六进制表示法”到底是什么意思?这通常是指编码二进制数据,而不是文本。您需要以某种方式对文本进行编码(例如,使用UTF-8),然后通过将每个字节转换为一对字符将二进制数据编码为文本。

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.

不,这样做根本不会缩小它。恰恰相反 - 你最终会得到更多的文字!但是,您可以压缩二进制表单。在将任意二进制数据表示为文本方面,Base64比普通十六进制更有效。使用Convert.ToBase64String和Convert.FromBase64String进行转换。

#2


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;
}

#3


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.

虽然我对C#实现无能为力,但我强烈推荐LZW作为一种易于实现的数据压缩算法供您使用。

#4


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.

如果我们问:如果你真的想做什么,也许可以更迅速地达成答案?将普通字符串转换为十六进制表示的字符串似乎是对任何事情的错误方法,除非您正在为Web创建十六进制/编码教程。

#5


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();
}

#1


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.

“十六进制表示法”到底是什么意思?这通常是指编码二进制数据,而不是文本。您需要以某种方式对文本进行编码(例如,使用UTF-8),然后通过将每个字节转换为一对字符将二进制数据编码为文本。

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.

不,这样做根本不会缩小它。恰恰相反 - 你最终会得到更多的文字!但是,您可以压缩二进制表单。在将任意二进制数据表示为文本方面,Base64比普通十六进制更有效。使用Convert.ToBase64String和Convert.FromBase64String进行转换。

#2


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;
}

#3


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.

虽然我对C#实现无能为力,但我强烈推荐LZW作为一种易于实现的数据压缩算法供您使用。

#4


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.

如果我们问:如果你真的想做什么,也许可以更迅速地达成答案?将普通字符串转换为十六进制表示的字符串似乎是对任何事情的错误方法,除非您正在为Web创建十六进制/编码教程。

#5


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();
}