Showing posts with label UTF-8. Show all posts
Showing posts with label UTF-8. Show all posts

Thursday, September 6, 2018

How do you correct, 'TypeError: Unicode-objects must be encoded before hashing'?

When you import 'hashlib' in Python and try to run the following statement you would get an error as shown:
----------
>>> print (hashlib.sha256("Hello World").hexdigest())
Traceback (most recent call last):
  File "", line 1, in
    print (hashlib.sha256("Hello World").hexdigest())
TypeError: Unicode-objects must be encoded before hashing
-----------

There are two ways you can correct this error:
---------
>>> print (hashlib.sha256(b"Hello World").hexdigest())
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
------------
>>> print (hashlib.sha256("Hello World".encode('utf-8')).hexdigest())
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
---------------------------
Python version used:
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

Monday, June 25, 2018

How do you print a Kannda (South Indian Language) word in Python using UTF-8?

Kannada (South Indian language) characters have the Unicode range from 0C80-0CCF 

UTF-8 is Python's default encoding.


 Let us take a simple word:
ಕಲಹ
Which means 'quarrel'. The letters are simple and you could use their Unicode equivalents
using this chunk of code block.

To form the word we concatenate the characters as shown,



What about a word such as this, ಅರಮನೆ? (means Palace)
The first three letters are simple but the fourth letter is a rendering with a diacritic of a simple letter 'ನ'. The fourth letter requires concatenating the two shown above:

In order to form the word ಅರಮನೆ we need to do the following:




Tuesday, October 27, 2015

How do you use the PHP Explode function?

This post describes some examples run on a Windows 10's Internet Information Services. This is relevant for PHP 4 and PHP 5.


First directly from documentation:
array explode ( string $delimiter , string $string [, int $limit ] )

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.


Parameters ¶

delimiter

The boundary string.

string

The input string.

limit


If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

Here is an example PHP file (a text file saved with the extension php) in the root directory (wwwroot) of Inetpub on a computer running Windows 10.
============
explodephp.php
============


Explode Function


$str="A thing of beauty, is a joy for ever!";
print_r(explode(" ",$str));
?>

-----------------
At first the file should not be saved to wwwroot with extension txt, it must be php

In the SaveAs window it can he saved as ANSI, Unicode, UTF-8 or Unicode Big Endian - you have to choose the correct one.

In the first position of the explode() function you need to choose the delimiter which can be, space, comma, etc and then the $str variable is to be set.

Here are some examples with parsed content. If there is no response as in some examples here, a FALSE is returned. Also if a delimiter is not present, the whole string is returned.


Parsing a string in Japanese