将段落转换为十六进制格式,然后返回字符串 [英] Converting a paragraph to hex notatation, then back to string

查看:160
本文介绍了将段落转换为十六进制格式,然后返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何将一个parapraph转换为十六进制符号,然后再回到它原来的字符串形式?

(C#)



附注:如果将字符串放在十六进制格式中,缩小它是最难以进入硬核收缩算法吗?

div>

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

 使用System; 
使用System.Text;

public class Hex
{
static void Main()
{
string original =快速的棕色狐狸跳过懒狗。

byte [] binary = Encoding.UTF8.GetBytes(original);
字符串十六进制= BytesToHex(二进制);
Console.WriteLine(Hex:{0},hex);
byte [] backToBinary = HexToBytes(hex);

string restored = Encoding.UTF8.GetString(backToBinary);
Console.WriteLine(已恢复:{0},已恢复);
}

private static readonly char [] HexChars =0123456789ABCDEF.ToCharArray();
$ b public static String BytesToHex(byte [] data)
{
StringBuilder builder = new StringBuilder(data.Length * 2);
foreach(数据中的字节b)
{
builder.Append(HexChars [b>> 4]);
builder.Append(HexChars [b& 0xf]);
}
return builder.ToString();

$ b $ public static byte [] HexToBytes(string text)
{
if((text.Length& 1)!= 0)
{
抛出新的ArgumentException(无效的十六进制:奇数长度);
}
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';如果(c> ='A'&& c< ='F')
{
return c -'A'+ 10;
}
;
}
if(c> ='a'&& c< ='f')
{
return c -'A'+ 10;
}
抛出新的ArgumentOutOfRangeException(无效的十六进制数字:+ c);


$ / code $ / pre

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


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?

解决方案

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.

这篇关于将段落转换为十六进制格式,然后返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆