位数组转换为字符串然后返回位数组 [英] Bit Array to String and back to Bit Array
问题描述
我正在使用霍夫曼编码对此处
其中的代码构建了一个霍夫曼树,用于对其进行编码和解码。
对于我来说,我需要获取压缩后的内容,将其存储并解压缩,然后
编码器的输出和解码器的输入是 BitArray
。
当我尝试将此 BitArray
转换为 String
并返回至 BitArray
并使用以下代码对其进行解码,我得到一个奇怪的答案。
树huffmanTree = new Tree();
huffmanTree.Build(输入);
字符串输入= Console.ReadLine();
BitArray编码= huffmanTree.Encode(输入);
//打印位
Console.Write(编码位:);
foreach(编码的布尔位)
{
Console.Write((bit?1:0)+);
}
Console.WriteLine();
//将位数组转换为字节
Byte [] e = new Byte [(encoded.Length / 8 +(encoded.Length%8 == 0?0:1)) ];
encoding.CopyTo(e,0);
//将字节转换为字符串
字符串输出= Encoding.UTF8.GetString(e);
//将字符串转换回字节
e =新的Byte [d.Length];
e = Encoding.UTF8.GetBytes(d);
//将字节转换回位数组
BitArray todecode = new BitArray(e);
已解码字符串= huffmanTree.Decode(todecode);
Console.WriteLine( Decoded: +已解码);
Console.ReadLine();
本教程是:
我的代码的输出为:
在哪里我错了朋友吗?帮帮我,谢谢。
您不能将任意字节填充到字符串中。这个概念只是不确定的。
字符串输出= Encoding.UTF8.GetString(e);
e
此时只是二进制垃圾,它不是UTF8字符串不是。因此,对它调用UTF8方法没有任何意义。
解决方案:请勿在字符串之间进行转换和反向转换。这不是往返。您为什么首先要这样做?如果您需要一个字符串,请使用可双向传输的格式,例如base-64或base-85。
Possible Duplicate Converting byte array to string and back again in C#
I am using Huffman Coding for compression and decompression of some text from here
The code in there builds a huffman tree to use it for encoding and decoding. Everything works fine when I use the code directly.
For my situation, i need to get the compressed content, store it and decompress it when ever need.
The output from the encoder and the input to the decoder are BitArray
.
When I tried convert this BitArray
to String
and back to BitArray
and decode it using the following code, I get a weird answer.
Tree huffmanTree = new Tree();
huffmanTree.Build(input);
string input = Console.ReadLine();
BitArray encoded = huffmanTree.Encode(input);
// Print the bits
Console.Write("Encoded Bits: ");
foreach (bool bit in encoded)
{
Console.Write((bit ? 1 : 0) + "");
}
Console.WriteLine();
// Convert the bit array to bytes
Byte[] e = new Byte[(encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1))];
encoded.CopyTo(e, 0);
// Convert the bytes to string
string output = Encoding.UTF8.GetString(e);
// Convert string back to bytes
e = new Byte[d.Length];
e = Encoding.UTF8.GetBytes(d);
// Convert bytes back to bit array
BitArray todecode = new BitArray(e);
string decoded = huffmanTree.Decode(todecode);
Console.WriteLine("Decoded: " + decoded);
Console.ReadLine();
The Output of Original code from the tutorial is:
The Output of My Code is:
Where am I wrong friends? Help me, Thanks in advance.
You cannot stuff arbitrary bytes into a string. That concept is just undefined. Conversions happen using Encoding.
string output = Encoding.UTF8.GetString(e);
e
is just binary garbage at this point, it is not a UTF8 string. So calling UTF8 methods on it does not make sense.
Solution: Don't convert and back-convert to/from string. This does not round-trip. Why are you doing that in the first place? If you need a string use a round-trippable format like base-64 or base-85.
这篇关于位数组转换为字符串然后返回位数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!