I try to compress some binary string with deflate32 but something is going wrong.
string myString = "101111110101010111111111111";
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(myString);
MemoryStream stream = new MemoryStream(byteArray);
var compressed = new DeflateStream(stream, CompressionLevel.Fastest);
Console.WriteLine(compressed); // Can't see any compressed string
Where is my mistake?
Related
I have a file with text in UTF16LE like so
feff 0074 0065 0073 0074
which is basically
test
What I am trying to is to read that file:
using (FileStream stream = File.Open(fileName, FileMode.Open))
{
byte[] b = new byte[stream.Length];
Encoding u16LE = Encoding.Unicode;
arrRaw.Add(u16LE.GetString(b));
}
After that im starting reading those string array in another function:
[![enter image description here][2]][2]
And so the output of that is just hieroglyphs. I don't know what I'm doing wrong. Moreover I was trying to start reading from the third byte in that array, but the result was the same
public static string Utf16ToUtf8Initial(string utf16String)
{
string utf8String = String.Empty;
byte[] utf16Bytes = Encoding.Unicode.GetBytes(utf16String);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
utf8String = Encoding.UTF8.GetString(utf8Bytes);
return utf8String;
}
I want to convert file into binary. I tried, but I get 0X000000000000000..... It is not correct. Always every file getting that digits. Please help me to solve thanks in advance
if (value.resume_file.CompareTo("") != 0)
{
byte[] binary = new byte[value.resume_file.Length];
//binary = Convert.ToByte(value.resume_file);
objJobSeekers.IsResume = true;
objJobSeekers.DocFileName = value.resume_file;
objJobSeekers.Resume = binary;
objJobSeekers.TypedResume = DBNull.Value;
}
string to byte[]
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
byte[] to string
str = System.Text.Encoding.UTF8.GetString(bytes);
Is this what you looking for?
If you want to read any file from disk and get its bytes then use
string FileDir = "D:\\File.doc";
byte[] MyBytes = File.ReadAllBytes(FileDir);
If you want to convert some random object in memory into bytes then you can use a BinarySerializer
byte[] MyBytes;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
MyBytes = ms.ToArray();
}
Sorry for the long post, will try to make this as short as possible.
I'm consuming a json API (which has zero documentation of course) which returns something like this:
{
uncompressedlength: 743637,
compressedlength: 234532,
compresseddata: "lkhfdsbjhfgdsfgjhsgfjgsdkjhfgj"
}
The data (xml in this case) is compressed and then base64 encoded data which I am attempting to extract. All I have is their demo code written in perl to decode it:
use Compress::Zlib qw(uncompress);
use MIME::Base64 qw(decode_base64);
my $uncompresseddata = uncompress(decode_base64($compresseddata));
Seems simple enough.
I've tried a number of methods to decode the base64:
private string DecodeFromBase64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);
return returnValue;
}
public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
And I have tried using Ionic.Zip.dll (DotNetZip?) and zlib.net to inflate the Zlib compression. But everything errors out. I am trying to track down where the problem is coming from. Is it the base64 decode or the Inflate?
I always get an error when inflating using zlib: I get a bad Magic Number error using zlib.net and I get "Bad state (invalid stored block lengths)" when using DotNetZip:
string decoded = DecodeFromBase64(compresseddata);
string decompressed = UnZipStr(GetBytes(decoded));
public static string UnZipStr(byte[] input)
{
using (MemoryStream inputStream = new MemoryStream(input))
{
using (Ionic.Zlib.DeflateStream zip =
new Ionic.Zlib.DeflateStream(inputStream, Ionic.Zlib.CompressionMode.Decompress))
{
using (StreamReader reader =
new StreamReader(zip, System.Text.Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
After reading this:
http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
And listening to one of the comments. I changed the code to this:
MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata));
memStream.ReadByte();
memStream.ReadByte();
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress);
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd();
And it's working fine.
This was the culprit:
http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
With skipping the first two bytes I was able to simplify it to:
MemoryStream memStream = new MemoryStream(Convert.FromBase64String(compresseddata));
memStream.ReadByte();
memStream.ReadByte();
DeflateStream deflate = new DeflateStream(memStream, CompressionMode.Decompress);
string doc = new StreamReader(deflate, System.Text.Encoding.UTF8).ReadToEnd();
First, use System.IO.Compression.DeflateStream to re-inflate the data. You should be able to use a MemoryStream as the input stream. You can create a MemoryStream using the byte[] result of Convert.FromBase64String.
You are likely causing all kinds of trouble trying to convert the base64 result to a given encoding; use the raw data directly to Deflate.
This question already has answers here:
How do I generate a stream from a string?
(12 answers)
Closed 9 years ago.
I need to convert a String to System.IO.Stream type to pass to another method.
I tried this unsuccessfully.
Stream stream = new StringReader(contents);
Try this:
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
and
// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
To convert a string to a stream you need to decide which encoding the bytes in the stream should have to represent that string - for example you can:
MemoryStream mStrm= new MemoryStream( Encoding.UTF8.GetBytes( contents ) );
MSDN references:
http://msdn.microsoft.com/en-us/library/ds4kkd55%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/e55f3s5k.aspx
System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes( contents));
string str = "asasdkopaksdpoadks";
byte[] data = Encoding.ASCII.GetBytes(str);
MemoryStream stm = new MemoryStream(data, 0, data.Length);
this is old but for help :
you can also use the stringReader stream
string str = "asasdkopaksdpoadks";
StringReader TheStream = new StringReader( str );
I have a JSON string in a MemoryStream. I am using the following code to get it out as an ASCII string:
MemoryStream memstream = new MemoryStream();
/* Write a JSON string to memstream here */
byte[] jsonBytes = new byte[memstream.Length];
memstream.Read(jsonBytes, 0, (int)memstream.Length);
string jsonString = Encoding.ASCII.GetString(jsonBytes);
What is a shorter/shortest way to do this?
You could use the ToArray method:
using (var stream = new MemoryStream())
{
/* Write a JSON string to stream here */
string jsonString = System.Text.Encoding.ASCII.GetString(stream.ToArray());
}
new StreamReader(memstream, Encoding.ASCII).ReadToEnd()