Generic string encoder in C #

I need it very simply, but before I invent the wheel, I would like to know if there is something like that within the existing ones.

I would like to encode (and decrypt) strings from a table of predefined characters. I have many lines containing multiple characters. Here is the line I would like to encode:

cn = 1; pl = 23; 3 = VF; st = 0

This line size is 20 characters, so 20 bytes.

In the string, I use only the following characters: cn = 1; p23vf0

Only 11 characters. So each character can be encoded with 4 bits just not? Decrease the total number of bytes used for 10.

Is there any existing method in .NET that can take a string in a parameter and an array of a reference table and return encoded bytes?

char [] reference = "cn = 1; p23vf0" .ToCharArray (); string input = "cn = 1; pl = 23; vf = 3; vv = 0";

byte [] encoded = someClass.Encode (input, link); string decoded = someClass.Decode (encoded, link);

Assert.AreEqual (input, decoding);

+3
source share
7 answers

Any compression algorithm uses Huffman encoding . This is basically what you are looking for here. This encoding is not disclosed as a class separately, it is part of the DeflateStream and GZipStream class algorithm. This is what you should use if your strings are of reasonable size. If they are short, then they are not in the coding.

+2
source

The interrogative question ... There is nothing in the structure, but it can be done like this:

public static byte[] Encode(string input, string reference) {
  int size = 1;
  while ((1 << ++size) < reference.Length);
  byte[] result = new byte[(size * input.Length + 7) / 8];
  new BitArray(
    input
    .Select(c => {
      int index = reference.IndexOf(c);
      return Enumerable.Range(0, size).Select(i => (index & (1 << i)) != 0);
    })
    .SelectMany(a => a)
    .ToArray()
  ).CopyTo(result, 0);
  return result;
}

public static string Decode(byte[] encoded, int length, string reference) {
  int size = 1;
  while ((1 << ++size) < reference.Length);
  return new String(
    new BitArray(encoded)
      .Cast<bool>()
      .Take(length * size)
      .Select((b, i) => new { Index = i / size, Bit = b })
      .GroupBy(g => g.Index)
      .Select(g => reference[g.Select((b, i) => (b.Bit ? 1 : 0) << i).Sum()])
      .ToArray()
  );
}

, , , .

, , , , :

string reference = "cn=1;pl23vf0";
string input = "cn=1;pl=23;vf=3;vv=0";

byte[] encoded = Encode(input, reference);

, , :

string decoded = Decode(encoded, input.Length, reference);

( EOF , , base64 .)

+2

" ", , , BitArray.. -, .

// modify this as appropriate to divide your original input string...
public IEnumerable<string> Divide( string s )
{ 
    for( int i = 0; i < s.Length; i += 2 )
        yield return s.Substring( i, 2 );
}

public IEnumerable<bool> AsBoolArray( byte b )
{
    var i = 4; // assume we only want 4-bits
    while( i-- > 0 )
    {
        yield return (b & 0x01) != 0;
        b >>= 1;
    }
}

// define your own mapping table...
var mappingTable = 
  new Dictionary<string,int>() { {"cn", 1}, {"pl",23}, {"vf",3}, {"vv",0} /*...*/ };
var originalString = "cncnvfvvplvvplpl";

// encode the data by mapping each string to the dictionary...
var encodedData = DivideString( originalString ).Select( s => mappingTable[s] );
// then convert into a bitVector based on the boolean representation of each value...
// The AsBoolArray() method return the 4-bit encoded bool[] for each value
var packedBitVector = 
  new BitArray( encodedData.Select( x => AsBoolArray(x) ).ToArray() );
// you can use BitArray.CopyTo() to get the representation out as a packed int[]
+1

, , System.IO.Compression.GZipStream . , , , 2 .

+1

There is nothing like this in the base class library. You will have to create your own.

Take a look at the Encoder class from System.Text - some elements may help.

0
source

Could any StringBuilder help anymore ?

0
source

You can use CrytpAPI. here is a good example, including string encryption and decryption methods. I do not think that it “compresses” your data for you.

0
source

Source: https://habr.com/ru/post/1736182/


All Articles