RSSAll Entries Tagged With: "encode"

Computation of hashes

Generating hashing for files or keys is often used in programming to identify files, tables or other data collection types. There are different ways of doing this and some are efficient meaning the method of hashing is reliable unlike others which may generate similar keys for different types of bytes.

/// <summary>
/// [...]

Different ways to generate MD5 Hash in PHP

The example below shows three ways to produce the exact same MD5 hash in PHP5.

// PHP’s basic md5() function
$hashA = md5(’encodeme’);
 
// MD5 by way of the mhash extension
$hashB = bin2hex(mhash(MHASH_MD5, ‘encodeme’));
 
// MD5 with the hash() function in PHP 5.1.2+
$hashC = hash(’md5′, ‘encodeme’);

$hashA, $hashB, and $hashC are all the same.

Remove non ascii characters from string in PHP

The example below will remove any non-ASCII characters from a string.

<?php
$str = "Remove these: รครณ";
$string = preg_replace(’/[^(\x20-\x7F)]*/’,”, $str);
?>

php function to encode string to utf8

use the following php function to convert a string to utf8 encoding.

// Fixes the encoding to uf8
function fixEncoding($in_str)
{
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
} // fixEncoding

Encode url in EXTJS

We can encode urls in EXTJS using the Ext.urlEncode() function.
Example:
Sample Object:

var selectedFood = {food1:’Dal’, food2:’Naan’,food3:’Rice’};

Convert the object to URL data:

var encodedUrl = Ext.urlEncode(selectedFood);
// encodedUrl is an encoded URL query string://food1=Dal&food2=Naan&food3=Rice

Encode array to JSON in EXTJS

1. Create an array called foodArray:

var foodArray = new Array();

2. Add values in the array:

foodArray[0] = ‘Dal’;
foodArray[1] = ‘Naan’;
foodArray[2] = ‘Rice’;

3. Now, convert to JSON:

var foodJson = Ext.encode(foodArray);

Encode session data to a string using PHP

Developers may want to save all the current session variables in string format and extract them back into variables at a later point. This can be achieved using session_encode and session_decode functions.

<?php
session_register("my_items");
$my_items = array("one", "two");
 
$string_items = session_encode();
$fp = @fopen ("save_items.txt", "w")
or [...]

Encode a bitmap file into a string using Base64 in C#

byte[] image = null;
using (FileStream filestrm = new FileStream(@"C:\mypic.bmp",FileMode.Open, FileAccess.Read))
{
using (BinaryReader reader = new BinaryReader(filestrm))
{
img = new byte[reader.BaseStream.Length];
for (int i = 0; i < reader.BaseStream.Length; i++)
{
img[i] = reader.ReadByte( );
}
}
}
string ConvertedbmpAsString = img.Base64EncodeBytes();

Encode binary data As Base64 in C#

Generally a byte[] representing some binary information, such as a bitmap is encoded into a string so that it can be sent over a binary-unfriendly transport, such as email, using Base64.
The static method Convert.ToBase64String on the Convert class can be used on a byte[] to encode to its String equivalent
Example:

using System;
using System.IO;
static class MyModClass
{
public static [...]

Create an XML-RPC Request with xmlrpc_encode_request

The process to create an XML-RPC request can be broken up into individual steps:

Serialize the PHP data into XML-RPC format

Create an XML-RPC request by combining HTTP headers and serialized data

Make the call to the service

The PHP function xmlrpc_encode_request can be used to serialize data and it creates the entire XML-RPC request call starting with the [...]