Search results for 'Base64Decode'. 2 post(s) found.
- 2009/07/16 Base64 Encoding/Decoding function for Delphi
- 2007/09/25 Compress and decompress strings in C#
Base64Encoding function is frequently used in MIME Type based programming. Here's the good resource for supporting Base64Encode and Base64Decode.
uses IdCoder, IdCoder3to4, IdCoderMIME, jpeg;
function Base64Decode(const Text : ansiString): ansiString;
var
Decoder : TIdDecoderMime;
begin
Decoder := TIdDecoderMime.Create(nil);
try
Result := Decoder.DecodeString(Text);
finally
FreeAndNil(Decoder)
end
end;
function Base64Encode(const Text : ansiString): ansiString;
var
Encoder : TIdEncoderMime;
begin
Encoder := TIdEncoderMime.Create(nil);
try
Result := Encoder.EncodeString(Text);
finally
FreeAndNil(Encoder);
end
end;
function Base64Decode(const Text : ansiString): ansiString;
var
Decoder : TIdDecoderMime;
begin
Decoder := TIdDecoderMime.Create(nil);
try
Result := Decoder.DecodeString(Text);
finally
FreeAndNil(Decoder)
end
end;
function Base64Encode(const Text : ansiString): ansiString;
var
Encoder : TIdEncoderMime;
begin
Encoder := TIdEncoderMime.Create(nil);
try
Result := Encoder.EncodeString(Text);
finally
FreeAndNil(Encoder);
end
end;
I verified above functions on Delphi 2009.
Another posts included in "Delphi"
| How to encode subject content when sending mail with TIdMessage ? (0) | 2009/07/16 |
| How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL (0) | 2009/07/20 |
| When subject is crashing in TIdSMTP VCL (0) | 2009/07/20 |
| How to send email by TIdSMTP VCL ? (0) | 2009/07/07 |
| How to return exit code such as exit() function in C/C++ ? (0) | 2009/07/02 |
| How to get parameter string ? (0) | 2009/06/30 |
| Save TBitmap image to Jpeg format image in Delphi (0) | 2009/06/26 |
| How to get screen resolution in case of using multiple monitors ? (0) | 2008/12/16 |
Trackback : Cannot send a trackbact to this post.
A couple of days ago, I had to store some very big strings in an XML file. To keep the file size down I wanted to compress the strings using a GZipStream and then decompress them later when needed.
I modified the methods from Papenoo pa so they handled strings in stead of byte arrays.
using System.IO.Compression;
using System.Text;
using System.IO;
public static string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String (gzBuffer);
}
public static string Decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
using System.Text;
using System.IO;
public static string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String (gzBuffer);
}
public static string Decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
The strings need to be longer than 3-400 characters; otherwise the compression rate is not good enough.
Another posts included in "C#"
| How to restrict a program to a single instance (0) | 2007/09/25 |
| How to exit from a console application (0) | 2007/09/25 |
| The best way to enable and disable buttons, menu items and toolbar buttons (0) | 2007/09/25 |
| Graphics in C# - Irregular Forms (0) | 2007/09/25 |
| Extracting the Country from IP Address (0) | 2007/09/25 |
| String Replace Function For C# (0) | 2007/08/29 |
| How to print character in C# by ASCII code (0) | 2007/08/29 |
| Adding Custom Paper Sizes To Named Printers (0) | 2007/08/28 |
Trackback : Cannot send a trackbact to this post.
-
Subject different money making ideas
2010/01/29 05:57
moneyideas
-
Subject different money making ideas
2010/01/29 14:31
moneyideas
-
Subject different money making ideas
2010/01/31 16:41
moneyideas

Prev

Rss Feed