Featured Posts

How to Store Binary Data in Strings using PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 08-12-2009

0

You want to parse a string that contains values encoded as a binary structure or encode values into a string. For example, you want to store numbers in their binary representation instead of as sequences of ASCII characters.

1.16.2. Solution

Use pack( ) to store binary data in a string:

<?php
$packed = pack('S4',1974,106,28225,32725);
?>

Use unpack( ) to extract binary data from a string:

<?php
$nums = unpack('S4',$packed);
?>

1.16.3. Discussion

The first argument to pack( ) is a format string that describes how to encode the data that’s passed in the rest of the arguments. The format string S4 tells pack( ) to produce four unsigned short 16-bit numbers in machine byte order from its input data. Given 1974, 106, 28225, and 32725 as input on a little-endian machine, this returns eight bytes: 182, 7, 106, 0, 65, 110, 213, and 127. Each two-byte pair corresponds to one of the input numbers: 7 * 256 + 182 is 1974; 0 * 256 + 106 is 106; 110 * 256 + 65 = 28225; 127 * 256 + 213 = 32725.

The first argument to unpack( ) is also a format string, and the second argument is the data to decode. Passing a format string of S4, the eight-byte sequence that pack( ) produced returns a four-element array of the original numbers. print_r($nums) prints:

Array
(
    [1] => 1974
    [2] => 106
    [3] => 28225
    [4] => 32725
)

In unpack( ), format characters and their count can be followed by a string to be used as an array key. For example:

<?php
$nums = unpack('S4num',$packed);
print_r($nums);
?>

This prints:

Array
(
    [num1] => 1974
    [num2] => 106
    [num3] => 28225
    [num4] => 32725
)

Multiple format characters must be separated with / in unpack( ):

<?php
$nums = unpack('S1a/S1b/S1c/S1d',$packed);
print_r($nums);
?>

This prints:

Array
(
    [a] => 1974
    [b] => 106
    [c] => 28225
    [d] => 32725
)
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

What is Dead-Character Messages in Visual Programming

Posted by admin | Posted in Theory Subjects, Visual Programming | Posted on 12-11-2009

0

Windows programs can usually ignore WM_DEADCHAR and WM_SYSDEADCHAR messages, but you should definitely know what dead characters are and how they work.

On some non-U.S. English keyboards, certain keys are defined to add a diacritic to a letter. These are called “dead keys” because they don’t generate characters by themselves. For instance, when a German keyboard is installed, the key that is in the same position as the +/= key on a U.S. keyboard is a dead key for the grave accent (`) when shifted and the acute accent (´) when unshifted.

When a user presses this dead key, your window procedure receives a WM_DEADCHAR message with wParam equal to ASCII or Unicode code for the diacritic by itself. When the user then presses a letter key that can be written with this diacritic (for instance, the A key), the window procedure receives a WM_CHAR message where wParam is the ANSI code for the letter `a’ with the diacritic.

Thus, your program does not have to process the WM_DEADCHAR message because the WM_CHAR message gives the program all the information it needs. The Windows logic even has built-in error handling: If the dead key is followed by a letter that can’t take a diacritic (such as `s’), the window procedure receives two WM_CHAR messages in a row—the first with wParam equal to the ASCII code for the diacritic by itself (the same wParam value delivered with the WM_DEADCHAR message) and the second with wParam equal to the ASCII code for the letter `s’.

Of course, the best way to get a feel for this is to see it in action. You need to load a foreign keyboard that uses dead keys, such as the German keyboard that I described earlier. You do this in the Control Panel by selecting Keyboard and then the Language tab. Then you need an application that shows you the details of every keyboard message a program can receive.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)