本文介绍了制作字符串Shoreter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单如果我有30个字符串就像这样。

字符串str1 =qwertyuiop12345asdfghjkl;

我怎么能像je5J那样小一些?

Simply If I have string of 30 Char Like This.
string str1="qwertyuiop12345asdfghjkl;"
how I can Make it smalle like "je5J" as Example?

推荐答案


public string CompressString(string str)
{
    var sb = new StringBuilder();
    foreach (var chr in str)
    {
        sb.Append(Convert.ToString(chr, 2).PadLeft(7,'0'));
    }

    int width = (int)(Math.Ceiling((double)sb.Length / 8)) * 8;
    string bits = sb.ToString().PadLeft(width, '0');

    var list = Enumerable
           .Range(0, bits.Length / 8)
           .Select(i => bits.Substring(i * 8, 8))
           .ToList();

    var ascii = new string(list.Select(p => (char)Convert.ToByte(p, 2)).ToArray());
    return ascii;
}

public string DecompressString(string str)
{
    var sb = new StringBuilder();
    foreach (var chr in str)
    {
        sb.Append(Convert.ToString(chr, 2).PadLeft(8,'0'));
    }

    string bits = sb.ToString();
    bits = bits.Remove(0, bits.Length % 7);

    var list = Enumerable
          .Range(0, bits.Length / 7)
          .Select(i => bits.Substring(i * 7, 7).PadLeft(8,'0'))
          .ToList();

    var ascii = new string(list.Select(p => (char)Convert.ToByte(p, 2)).ToArray());
    return ascii;
}



这篇关于制作字符串Shoreter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:55