wxyz2010's blog

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  7 Posts :: 3 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2009年12月31日 #

作者:终南   <li.zhongnan@hotmail.com>

1、介绍

Windows操作系统是应用最关的操作系统,因此动态链接库也为程序员所熟悉,即使对于普通的使用者来说,很多时候也会碰到.dll结尾的文件, 这就是动态链接库文件。Windows下的动态链接库可以通过参考头文件和.lib库文件进行编译,从而使得动态链接库隐式地被使用;也可以使用 LoadLibrary、GetProcAddress等函数来显式调用动态链接库。

2、语法、导入导出

在Windows编程中,对于要使用或被使用的函数或者变量,需要使用 __declspec 关键字来声明,以告诉编译器该变量或函数不是普通的变量或函数,而是一个动态链接库的接口属性。

如果定义一个要被其他代码使用的函数,可以写成:
__declspec( dllexport ) int add(int a, int b);

如果在该代码中,打算使用另外一个程序中的变量,则可以写成:
__declspec( dllimport ) char *name;

动态链接库通常包含一系列供其他程序使用函数,因此 declspec( dllexport ) 语法形式最为常用。如果动态库需要其他程序中的定义的全局变量,则需要在其他程序中使用导出该变量,在动态链接库中则需要使用 extern declspec( dllexport ) 将该变量声明为外部变量以便使用。

3、链接方式

可以以下列两种方式之一链接到(或加载)DLL:

隐式链接
显式链接

隐式链接有时称为静态加载或加载时动态链接。显式链接有时称为动态加载或运行时动态链接。在隐式链接下,使用 DLL 的可执行文件链接到该 DLL 的创建者所提供的导入库(.lib 文件)。使用 DLL 的可执行文件加载时,操作系统加载此 DLL。客户端可执行文件调用 DLL 的导出函数,就好像这些函数包含在可执行文件内一样。

在显式链接下,使用 DLL 的可执行文件必须进行函数调用以显式加载和卸载该 DLL,并访问该 DLL 的导出函数。客户端可执行文件必须通过函数指针调用导出函数。可执行文件对两种链接方法可以使用同一个 DLL。另外,由于一个可执行文件可隐式链接到某个 DLL,而另一个可显式附加到此 DLL,故这些机制不是互斥的。

4、隐式链接

隐式链接动态链接库比较简单,不予详述。

5、显式链接API函数

显式链接主要涉及到3个API函数( LoadLibrary , GetProcAddress 和 FreeLibrary ),要使用这些函数包含windows.h头文件即可。

(1)HINSTANCE LoadLibrary(LPCSTR lpLibFileName);

该函数用来加载指定动态库文件,并且返回句柄。

参数lpLibFileName为动态链接库的名称。Windows 首先搜索“已知 DLL”,如 Kernel32.dll 和 User32.dll。然后按下列顺序搜索 DLL:

1、当前进程的可执行模块所在的目录。
2、当前目录。
3、Windows 系统目录。GetSystemDirectory 函数检索此目录的路径。
4、Windows 目录。GetWindowsDirectory 函数检索此目录的路径。
5、PATH 环境变量中列出的目录。

(2)FARPROC GetProcAddress (HMODULE hModule, LPCSTR lpProcName);

函数GetProcAddress 用来获取 DLL 导出函数的地址。返回由lpProcName指定的变量或函数指针。

参数hModule为已经加载的动态库文件的句柄。

参数lpProcName为要调用的变量或函数名称。

(3)BOOL FreeLibrary(HMODULE hModule);
从内存中释放hModule所代表的动态链接库。

(4)如果发生错误,可以调用GetLastError()函数或去错误代码。

6、显示链接举例

(1)动态库文件代码:dll_demo.c

#include <stdio.h>

__declspec( dllexport ) int add(int a, int b)
{
printf("calling add\n");
return a + b;
}

该文件中的add()函数计算两个整数之和,并且返回之前打印提示字符串。函数使用 __declspec( dllexport ) 语法来说明函数add(int a, int b)要被导出。

(2)客户端事例代码:main.c

#include <stdio.h>
#include <windows.h>

int main (int argc, char *argv[])
{
int a = 10, b = 20;
int c = 0;
HINSTANCE   hInstLibrary   =   NULL;
int (*add)();

printf ("Load DLL file\n");
if ((hInstLibrary = LoadLibrary(L"dll_demo.dll")) == NULL)
{
   printf ("***LoadLibrary ERROR: %d.\n", GetLastError());
   return 1;
}
if((add = (int (*)())GetProcAddress(hInstLibrary, "add")) == NULL) {
   printf ("***GetProcAddress ERROR: %d.\n", GetLastError());
   return 1;
}

c = add(a, b);
printf("%d + %d = %d\n", a, b, c);

FreeLibrary(hInstLibrary);
return 0;
}

程序利用LoadLibrary函数加载动态链接dll_demo.dll,利用FreeLibrary关闭句柄,利用GetLastError()获取错误代码,利用GetProcAddress定位共享库中的add函数,然后调用该函数执行加法计算,并打印结果。

(3)编译与运行

编译共享库:

在VS.Net中创建一个动态链接库项目,名称为dll_demo,加入文件dll_demo.c,编译后生成dll_demo.dll文件。

编译事例程序:

在VS.Net中创建一个动态链接库项目,名称为dll_main,加入文件main.c,编译后生成dll_main.exe可以执行文件。

运行:

将 dll_demo.dll 和 dll_main.exe 放在同一个目录下,然后双击运行 dll_main.exe。

输出:

Load DLL file
calling add
10 + 20 = 30

7、调用动态链接库中的变量

也可以使用动态链接库中的变量。例如,在动态链接库中定义:

__declspec( dllexport ) int num = 100;

那么可以在事例程序中这样调用:

int *d;
d = (int *)GetProcAddress(hInstLibrary, "num");
printf("num = %d\n", *d);
  

posted @ 2009-12-31 10:10 wxyz2010 阅读(343) | 评论 (0)编辑 收藏

2009年12月29日 #

The status of printers and print jobs are updated by the Win32 Spooler during the despool of a print job. At all other times, when that printer is not despooling and reports no state information, the printer is considered to be ready and idle.

MORE INFORMATION
As referred to by the Win32 API, a "printer" is comprised of the printer driver...

As referred to by the Win32 API, a "printer" is comprised of the printer driver, the print queue, and the input/output path to the physical printer. The operating system treats a physical printer as merely the destination of a print job generated by and passed through a system "Printer," referred to in the rest of this article as a Printer.

The most visible part of a Printer is a print queue. It is managed by the Print Manager or the Printer folders in the Windows 95-style user interfaces. The printer driver is the interface to the Printer that is used by applications to create print jobs via printer DCs. The I/O path for a Printer consists of several layers of system code culminating with a port monitor.

The port monitor is the interface to the physical printer at the down- stream end of a system Printer and is responsible for transferring the data of a print job across whatever connection exists to the physical printer. In the case of bi-directional printers, the port monitor would be responsible for transferring data to and from the physical printer. This connection, and the physical printer, are where errors occur. It is the job of the port monitor to report those errors.

The Spooler does not query for the state of a physical printer to which a Printer is connected. Instead, the state of a physical printer determines the success of a print job at the time it is despooled over the port monitor. If some error occurs in this process, the error is reported by the port monitor and recorded in a print job's status information. The Spooler, in turn, propagates reasonable error information to the Printer Queue.

Consequently, a system Printer reports no status when the Printer queue is empty. In this state, the Printer is assumed ready to accept print jobs. This is a valid assumption even if the physical printer is in an error state such as off-line. The operating system considers the Printer ready to accept print jobs even if, for some reason, it cannot complete delivery to the physical printer. Such a circumstance is considered an error state in the operating system that must be addressed by the user. It is not considered an error reportable to the application that is allowed to complete the spooling of the print job successfully.

Determining the State of a Physical Printer

There is one fundamental premise that must be true to determine the state of a physical printer: the Spooler must be attempting to send a print job to the physical printer. This is the only time the state of the printer is reported by the port monitor. In addition, the most meaningful information may be reported in the status members of a JOB_INFO structure for that particular print job because some port monitor will have set these values directly.

The JOB_INFO structures contain a Status member and a pStatus member. Both members contain status information of a print job reported by the port monitor. These two members differ in that the Status member is a bit field of states that contains predetermined values, while the pStatus member is a pointer to a string that could contain just about anything. These values are documented by the Win32 SDK and the WinSpool.h header file. The pStatus member is sometimes, but not always, set to a descriptive status string. The contents of this string are defined by each port monitor.

JOB_INFO structures are returned by two API functions: GetJob and EnumJobs. EnumJobs returns an array of JOB_INFO structures without requiring that the caller reference a particular job in the Printer queue. The print job that is currently despooling (printing) contains the status information. To find this job in the array, search the array of JOB_INFO structures to locate the print job whose Status member has the JOB_STATUS_PRINTING bit set.

An easier method of determining the printer status is to examine the Status member of a PRINTER_INFO structure. This structure is returned by the GetPrinter function. There is a disadvantage to this approach in that there is no pStatus string member in a PRINTER_INFO structure that might provide more detailed or extensive state information. However, there is an advantage in that a port monitor may set some of the more extensive printer status bits of the PRINTER_INFO structure. Note, however, that the default port monitor for Windows does not usually set more than the PRINTER_STATUS_ERROR bit of a Printer's Status member.

Note that the Status members of either set of structures may contain state information that is not strictly related to the physical printer. For example, the Status member of the PRINTER_INFO structures may be set with PRINTER_STATUS_PAUSED or PRINTER_STATUS_PENDING_DELETION, that are strictly relevant to the Print Queue. Also, the Status member of the JOB_INFO structure may contain state values for JOB_STATUS_PAUSED or JOB_STATUS_DELETING, that are relevant only to that particular print job. Note, also, that print jobs may accumulate in a Print queue after they have despooled and would be left with a state of JOB_STATUS_PRINTED.

Each of these functions requires a handle to a printer to identify the desired Printer. This handle is obtained from the OpenPrinter function, that accepts a string containing the name of the printer. This name can be either the local name of the printer or a UNC share name to a network printer.

The following sample code demonstrates how to call the EnumJobs function properly to retrieve JOB_INFO structures and how to call the GetPrinter function to retrieve PRINTER_INFO structures:

Sample Code

   BOOL GetJobs(HANDLE hPrinter,        /* Handle to the printer. */ 

JOB_INFO_2 **ppJobInfo, /* Pointer to be filled. */
int *pcJobs, /* Count of jobs filled. */
DWORD *pStatus) /* Print Queue status. */

{

DWORD cByteNeeded,
nReturned,
cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;

/* Get the buffer size needed. */
if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
}

pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if (!(pPrinterInfo))
/* Failure to allocate memory. */
return FALSE;

/* Get the printer information. */
if (!GetPrinter(hPrinter,
2,
(LPSTR)pPrinterInfo,
cByteNeeded,
&cByteUsed))
{
/* Failure to access the printer. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}

/* Get job storage space. */
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
NULL,
0,
(LPDWORD)&cByteNeeded,
(LPDWORD)&nReturned))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
}

pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
if (!pJobStorage)
{
/* Failure to allocate Job storage space. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}

ZeroMemory(pJobStorage, cByteNeeded);

/* Get the list of jobs. */
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
(LPBYTE)pJobStorage,
cByteNeeded,
(LPDWORD)&cByteUsed,
(LPDWORD)&nReturned))
{
free(pPrinterInfo);
free(pJobStorage);
pJobStorage = NULL;
pPrinterInfo = NULL;
return FALSE;
}

/*
* Return the information.
*/
*pcJobs = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);

return TRUE;

}

BOOL IsPrinterError(HANDLE hPrinter)
{

JOB_INFO_2 *pJobs;
int cJobs,
i;
DWORD dwPrinterStatus;

/*
* Get the state information for the Printer Queue and
* the jobs in the Printer Queue.
*/
if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
return FALSE;

/*
* If the Printer reports an error, believe it.
*/
if (dwPrinterStatus &
(PRINTER_STATUS_ERROR |
PRINTER_STATUS_PAPER_JAM |
PRINTER_STATUS_PAPER_OUT |
PRINTER_STATUS_PAPER_PROBLEM |
PRINTER_STATUS_OUTPUT_BIN_FULL |
PRINTER_STATUS_NOT_AVAILABLE |
PRINTER_STATUS_NO_TONER |
PRINTER_STATUS_OUT_OF_MEMORY |
PRINTER_STATUS_OFFLINE |
PRINTER_STATUS_DOOR_OPEN))
{
free( pJobs );
return TRUE;
}

/*
* Find the Job in the Queue that is printing.
*/
for (i=0; i < cJobs; i++)
{
if (pJobs[i].Status & JOB_STATUS_PRINTING)
{
/*
* If the job is in an error state,
* report an error for the printer.
* Code could be inserted here to
* attempt an interpretation of the
* pStatus member as well.
*/
if (pJobs[i].Status &
(JOB_STATUS_ERROR |
JOB_STATUS_OFFLINE |
JOB_STATUS_PAPEROUT |
JOB_STATUS_BLOCKED_DEVQ))
{
free( pJobs );
return TRUE;
}
}
}

/*
* No error condition.
*/
free( pJobs );
return FALSE;

}
NOTE: When printer pooling is enabled on Windows NT, there may be more than one print job despooling from a Printer queue that will report a status. This sample code does not consider that circumstance.
posted @ 2009-12-29 21:18 wxyz2010 阅读(591) | 评论 (0)编辑 收藏

Proper use of the Win32 Spooler Enumeration APIs requires two calls to the desired function. These APIs generally fill out an array of structures. However, the structures usually include pointers to strings or to other data. This extraneous data must also be stored in the return memory, so the strings and other data are appended to the array. Therefore, simply declaring an array of such structures on the stack would not set aside enough memory to hold all of the information the API returns.

MORE INFORMATION
The Enumeration APIs that behave this way include: EnumForms(), EnumJobs(), Enu...

The Enumeration APIs that behave this way include: EnumForms(), EnumJobs(), EnumMonitors(), EnumPorts(), EnumPrinterDrivers(), EnumPrinters(), and EnumPrintProcessors(). Also, GetJob(), GetPrinter(), and DocumentProperties() require the same treatment. In each case, proper usage requires an initial call to the function to determine the necessary buffer size, and a subsequent call that passes in a pointer to a dynamically-allocated buffer of sufficient size. The code below, from a console application, demonstrates this approach using the EnumJobs() API:
   BOOL ListJobsForPrinter( LPTSTR szPrinterName )
{

HANDLE hPrinter;
DWORD dwNeeded, dwReturned, i;
JOB_INFO_1 *pJobInfo;

// You need a printer handle, open the printer
if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
return FALSE;

// First you call EnumJobs() to find out how much memory you need
if( ! EnumJobs( hPrinter, 0, 0xFFFFFFFF, 1, NULL, 0, &dwNeeded,
&dwReturned ) )
{
// It should have failed, but if it failed for any reason other
// than "not enough memory", you should bail out
if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
ClosePrinter( hPrinter );
return FALSE;
}
}
// Allocate enough memory for the JOB_INFO_1 structures plus
// the extra data - dwNeeded from the previous call tells you
// the total size needed
if( (pJobInfo = (JOB_INFO_1 *)malloc( dwNeeded )) == NULL )
{
ClosePrinter( hPrinter );
return FALSE;
}
// Call EnumJobs() again and let it fill out our structures
if( ! EnumJobs( hPrinter, 0, 0xFFFFFFFF, 1, (LPBYTE)pJobInfo,
dwNeeded, &dwNeeded, &dwReturned ) )
{
ClosePrinter( hPrinter );
free( pJobInfo );
return FALSE;
}
// You're done with the printer handle, close it
ClosePrinter( hPrinter );

// dwReturned tells how many jobs there are
// Here, you'll simply display the number of jobs found
printf( "%d jobs\n", dwReturned );
// It's easy to loop through the jobs and access each one
for(i=0;i<dwReturned;i++)
{
// pJobInfo[i] is a JOB_INFO_1 struct for that job
// so here you could do whatever you want for each job
printf( "[%d] [%s]\n", pJobInfo[i].JobId, pJobInfo[i].pDocument );
}

// Clean up
free( pJobInfo );
return TRUE;
}
posted @ 2009-12-29 21:17 wxyz2010 阅读(431) | 评论 (0)编辑 收藏

2009年12月25日 #

Microsoft Algorithms and Programming Written TestAlgorithms and Programming

1. Given a rectangular (cuboidal for the puritans) cake with a
rectangular piece removed (any size or orientation), how would you cut
the
remainder of the cake into two equal halves with one straight cut of a
knife ?

2. You're given an array containing both positive and negative integers
and required to find the sub-array with the largest sum (O(N) a la
KBL). Write a routine in C for the above.

3. Given an array of size N in which every number is between 1 and N,
determine if there are any duplicates in it. You are allowed to destroy
the array if you like. [ I ended up giving about 4 or 5 different
solutions for this, each supposedly better than the others ].

4. Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without
making use of any floating point computations at all. [ This one had me
stuck for quite some time and I first gave a solution that did have
floating point computations ].

5. Given only putchar (no sprintf, itoa, etc.) write a routine putlong
that prints out an unsigned long in decimal. [ I gave the obvious
solution of taking % 10 and / 10, which gives us the decimal value in
reverse order. This requires an array since we need to print it out in
the
correct order. The interviewer wasn't too pleased and asked me to give
a
solution which didn't need the array ].

6. Give a one-line C expression to test whether a number is a power of
2. [No loops allowed - it's a simple test.]

7. Given an array of characters which form a sentence of words, give an
efficient algorithm to reverse the order of the words (not characters)
in it.

8. How many points are there on the globe where by walking one mile
south, one mile east and one mile north you reach the place where you
started.

9. Give a very good method to count the number of ones in a "n" (e.g.
32) bit number.

ANS. Given below are simple solutions, find a solution that does it in
log (n) steps.


Iterative

function iterativecount (unsigned int n)
begin
int count=0;
while (n)
begin
count += n & 0x1 ;
n >>= 1;
end
return count;
end

Sparse Count

function sparsecount (unsigned int n)
begin
int count=0;
while (n)
begin
count++;
n &= (n-1);
end
return count ;
end

10. What are the different ways to implement a condition where the
value of x can be either a 0 or a 1. Apparently the if then else
solution
has a jump when written out in assembly. if (x == 0) y=a else y=b There
is a logical, arithmetic and a data structure solution to the above
problem.

11. Reverse a linked list.

12. Insert in a sorted list

13. In a X's and 0's game (i.e. TIC TAC TOE) if you write a program for
this give a fast way to generate the moves by the computer. I mean this
should be the fastest way possible.

The answer is that you need to store all possible configurations of the
board and the move that is associated with that. Then it boils down to
just accessing the right element and getting the corresponding move for
it. Do some analysis and do some more optimization in storage since
otherwise it becomes infeasible to get the required storage in a DOS
machine.

14. I was given two lines of assembly code which found the absolute
value of a number stored in two's complement form. I had to recognize
what
the code was doing. Pretty simple if you know some assembly and some
fundaes on number representation.

15. Give a fast way to multiply a number by 7.

16. How would go about finding out where to find a book in a library.
(You don't know how exactly the books are organized beforehand).

17. Linked list manipulation.

18. Tradeoff between time spent in testing a product and getting into
the market first.

19. What to test for given that there isn't enough time to test
everything you want to.

20. First some definitions for this problem: a) An ASCII character is
one byte long and the most significant bit in the byte is always '0'.
b)
A Kanji character is two bytes long. The only characteristic of a Kanji
character is that in its first byte the most significant bit is '1'.

Now you are given an array of a characters (both ASCII and Kanji) and,
an index into the array. The index points to the start of some
character. Now you need to write a function to do a backspace (i.e.
delete the
character before the given index).

21. Delete an element from a doubly linked list.

22. Write a function to find the depth of a binary tree.

23. Given two strings S1 and S2. Delete from S2 all those characters
which occur in S1 also and finally create a clean S2 with the relevant
characters deleted.

24. Assuming that locks are the only reason due to which deadlocks can
occur in a system. What would be a foolproof method of avoiding
deadlocks in the system.

25. Reverse a linked list.

Ans: Possible answers -

iterative loop
curr->next = prev;
prev = curr;
curr = next;
next = curr->next
endloop

recursive reverse(ptr)
if (ptr->next == NULL)
return ptr;
temp = reverse(ptr->next);
temp->next = ptr;
return ptr;
end


26. Write a small lexical analyzer - interviewer gave tokens.
expressions like "a*b" etc.

27. Besides communication cost, what is the other source of
inefficiency in RPC? (answer : context switches, excessive buffer
copying). How
can you optimize the communication? (ans : communicate through shared
memory on same machine, bypassing the kernel _ A Univ. of Wash. thesis)

28. Write a routine that prints out a 2-D array in spiral order!

29. How is the readers-writers problem solved? - using semaphores/ada
.. etc.

30. Ways of optimizing symbol table storage in compilers.

31. A walk-through through the symbol table functions, lookup()
implementation etc. - The interviewer was on the Microsoft C team.

32. A version of the "There are three persons X Y Z, one of which
always lies".. etc..

33. There are 3 ants at 3 corners of a triangle, they randomly start
moving towards another corner.. what is the probability that they don't
collide.

34. Write an efficient algorithm and C code to shuffle a pack of
cards.. this one was a feedback process until we came up with one with
no
extra storage.

35. The if (x == 0) y = 0 etc..

36. Some more bitwise optimization at assembly level

37. Some general questions on Lex, Yacc etc.

38. Given an array t[100] which contains numbers between 1..99. Return
the duplicated value. Try both O(n) and O(n-square).

39. Given an array of characters. How would you reverse it. ? How would
you reverse it without using indexing in the array.

40. Given a sequence of characters. How will you convert the lower case
characters to upper case characters. ( Try using bit vector - solutions
given in the C lib -typec.h)

41. Fundamentals of RPC.

42. Given a linked list which is sorted. How will u insert in sorted
way.

43. Given a linked list How will you reverse it.

44. Give a good data structure for having n queues ( n not fixed) in a
finite memory segment. You can have some data-structure separate for
each queue. Try to use at least 90% of the memory space.

45. Do a breadth first traversal of a tree.

46. Write code for reversing a linked list.

47. Write, efficient code for extracting unique elements from a sorted
list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5,
9).

48. Given an array of integers, find the contiguous sub-array with the
largest sum.

ANS. Can be done in O(n) time and O(1) extra space. Scan array from 1
to n. Remember the best sub-array seen so far and the best sub-array
ending in i.

49. Given an array of length N containing integers between 1 and N,
determine if it contains any duplicates.

ANS. [Is there an O(n) time solution that uses only O(1) extra space
and does not destroy the original array?]

50. Sort an array of size n containing integers between 1 and K, given
a temporary scratch integer array of size K.

ANS. Compute cumulative counts of integers in the auxiliary array. Now
scan the original array, rotating cycles! [Can someone word this more
nicely?]

* 51. An array of size k contains integers between 1 and n. You are
given an additional scratch array of size n. Compress the original
array
by removing duplicates in it. What if k << n?

ANS. Can be done in O(k) time i.e. without initializing the auxiliary
array!

52. An array of integers. The sum of the array is known not to overflow
an integer. Compute the sum. What if we know that integers are in 2's
complement form?

ANS. If numbers are in 2's complement, an ordinary looking loop like
for(i=total=0;i< n;total+=array[i++]); will do. No need to check for
overflows!

53. An array of characters. Reverse the order of words in it.

ANS. Write a routine to reverse a character array. Now call it for the
given array and for each word in it.

* 54. An array of integers of size n. Generate a random permutation of
the array, given a function rand_n() that returns an integer between 1
and n, both inclusive, with equal probability. What is the expected
time of your algorithm?

ANS. "Expected time" should ring a bell. To compute a random
permutation, use the standard algorithm of scanning array from n downto
1,
swapping i-th element with a uniformly random element <= i-th. To
compute a
uniformly random integer between 1 and k (k < n), call rand_n()
repeatedly until it returns a value in the desired range.

55. An array of pointers to (very long) strings. Find pointers to the
(lexicographically) smallest and largest strings.

ANS. Scan array in pairs. Remember largest-so-far and smallest-so-far.
Compare the larger of the two strings in the current pair with
largest-so-far to update it. And the smaller of the current pair with
the
smallest-so-far to update it. For a total of <= 3n/2 strcmp() calls.
That's
also the lower bound.

56. Write a program to remove duplicates from a sorted array.

ANS. int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
} else
current++;

return insert;

}

57. C++ ( what is virtual function ? what happens if an error occurs in
constructor or destructor. Discussion on error handling, templates,
unique features of C++. What is different in C++, ( compare with unix).

58. Given a list of numbers ( fixed list) Now given any other list, how
can you efficiently find out if there is any element in the second list
that is an element of the first list (fixed list).

59. Given 3 lines of assembly code : find it is doing. IT was to find
absolute value.

60. If you are on a boat and you throw out a suitcase, Will the level
of water increase.

61. Print an integer using only putchar. Try doing it without using
extra storage.

62. Write C code for (a) deleting an element from a linked list (b)
traversing a linked list

63. What are various problems unique to distributed databases

64. Declare a void pointer ANS. void *ptr;

65. Make the pointer aligned to a 4 byte boundary in a efficient manner
ANS. Assign the pointer to a long number and the number with 11...1100
add 4 to the number

66. What is a far pointer (in DOS)

67. What is a balanced tree

68. Given a linked list with the following property node2 is left child
of node1, if node2 < node1 else, it is the right child.

O P
|
|
O A
|
|
O B
|
|
O C

How do you convert the above linked list to the form without disturbing
the property. Write C code for that.


O P
|
|
O B
/ \
/ \
/ \
O ? O ?

determine where do A and C go

69. Describe the file system layout in the UNIX OS

ANS. describe boot block, super block, inodes and data layout

70. In UNIX, are the files allocated contiguous blocks of data

ANS. no, they might be fragmented

How is the fragmented data kept track of

ANS. Describe the direct blocks and indirect blocks in UNIX file system

71. Write an efficient C code for 'tr' program. 'tr' has two command
line arguments. They both are strings of same length. tr reads an input
file, replaces each character in the first string with the
corresponding
character in the second string. eg. 'tr abc xyz' replaces all 'a's by
'x's, 'b's by 'y's and so on. ANS.
a) have an array of length 26.
put 'x' in array element corr to 'a'
put 'y' in array element corr to 'b'
put 'z' in array element corr to 'c'
put 'd' in array element corr to 'd'
put 'e' in array element corr to 'e'
and so on.

the code
while (!eof)
{
c = getc();
putc(array[c - 'a']);
}

72. what is disk interleaving

73. why is disk interleaving adopted

74. given a new disk, how do you determine which interleaving is the
best a) give 1000 read operations with each kind of interleaving
determine the best interleaving from the statistics

75. draw the graph with performance on one axis and 'n' on another,
where 'n' in the 'n' in n-way disk interleaving. (a tricky question,
should be answered carefully)

76. I was a c++ code and was asked to find out the bug in that. The bug
was that he declared an object locally in a function and tried to
return the pointer to that object. Since the object is local to the
function, it no more exists after returning from the function. The
pointer,
therefore, is invalid outside.

77. A real life problem - A square picture is cut into 16 squares and
they are shuffled. Write a program to rearrange the 16 squares to get
the original big square.

78.
int *a;
char *c;
*(a) = 20;
*c = *a;
printf("%c",*c);

what is the output?

79. Write a program to find whether a given m/c is big-endian or
little-endian!

80. What is a volatile variable?

81. What is the scope of a static function in C ?

82. What is the difference between "malloc" and "calloc"?

83. struct n { int data; struct n* next}node;
node *c,*t;
c->data = 10;
t->next = null;
*c = *t;
what is the effect of the last statement?

84. If you're familiar with the ? operator x ? y : z
you want to implement that in a function: int cond(int x, int y, int
z); using only ~, !, ^, &, +, |, <<, >> no if statements, or loops or
anything else, just those operators, and the function should correctly
return y or z based on the value of x. You may use constants, but only
8
bit constants. You can cast all you want. You're not supposed to use
extra variables, but in the end, it won't really matter, using vars
just
makes things cleaner. You should be able to reduce your solution to a
single line in the end though that requires no extra vars.

85. You have an abstract computer, so just forget everything you know
about computers, this one only does what I'm about to tell you it does.
You can use as many variables as you need, there are no negative
numbers, all numbers are integers. You do not know the size of the
integers,
they could be infinitely large, so you can't count on truncating at any
point. There are NO comparisons allowed, no if statements or anything
like that. There are only four operations you can do on a variable.
1) You can set a variable to 0.
2) You can set a variable = another variable.
3) You can increment a variable (only by 1), and it's a post increment.
4) You can loop. So, if you were to say loop(v1) and v1 = 10, your loop
would execute 10 times, but the value in v1 wouldn't change so the
first line in the loop can change value of v1 without changing the
number
of times you loop.
You need to do 3 things.
1) Write a function that decrements by 1.
2) Write a function that subtracts one variable from another.
3) Write a function that divides one variable by another.
4) See if you can implement all 3 using at most 4 variables. Meaning,
you're not making function calls now, you're making macros. And at most
you can have 4 variables. The restriction really only applies to
divide, the other 2 are easy to do with 4 vars or less. Division on the
other
hand is dependent on the other 2 functions, so, if subtract requires 3
variables, then divide only has 1 variable left unchanged after a call
to subtract. Basically, just make your function calls to decrement and
subtract so you pass your vars in by reference, and you can't declare
any new variables in a function, what you pass in is all it gets.
Linked lists

* 86. Under what circumstances can one delete an element from a singly
linked list in constant time?

ANS. If the list is circular and there are no references to the nodes
in the list from anywhere else! Just copy the contents of the next node
and delete the next node. If the list is not circular, we can delete
any but the last node using this idea. In that case, mark the last node
as dummy!

* 87. Given a singly linked list, determine whether it contains a loop
or not.

ANS. (a) Start reversing the list. If you reach the head, gotcha! there
is a loop!
But this changes the list. So, reverse the list again.
(b) Maintain two pointers, initially pointing to the head. Advance one
of them one node at a time. And the other one, two nodes at a time. If
the latter overtakes the former at any time, there is a loop!

p1 = p2 = head;

do {
p1 = p1->next;
p2 = p2->next->next;
} while (p1 != p2);

88. Given a singly linked list, print out its contents in reverse
order. Can you do it without using any extra space?

ANS. Start reversing the list. Do this again, printing the contents.

89. Given a binary tree with nodes, print out the values in
pre-order/in-order/post-order without using any extra space.

90. Reverse a singly linked list recursively. The function prototype is
node * reverse (node *) ;

ANS.

node * reverse (node * n)
{
node * m ;

if (! (n && n -> next))
return n ;

m = reverse (n -> next) ;
n -> next -> next = n ;
n -> next = NULL ;
return m ;
}

91. Given a singly linked list, find the middle of the list.

HINT. Use the single and double pointer jumping. Maintain two pointers,
initially pointing to the head. Advance one of them one node at a time.
And the other one, two nodes at a time. When the double reaches the
end, the single is in the middle. This is not asymptotically faster but
seems to take less steps than going through the list twice.

Bit-manipulation

92. Reverse the bits of an unsigned integer.

ANS.

#define reverse(x) \
(x=x>>16|(0x0000ffff&x)<<16, \
x=(0xff00ff00&x)>>8|(0x00ff00ff&x)<<8, \
x=(0xf0f0f0f0&x)>>4|(0x0f0f0f0f&x)<<4, \
x=(0xcccccccc&x)>>2|(0x33333333&x)<<2, \
x=(0xaaaaaaaa&x)>>1|(0x55555555&x)<<1)

* 93. Compute the number of ones in an unsigned integer.

ANS.

#define count_ones(x) \
(x=(0xaaaaaaaa&x)>>1+(0x55555555&x), \
x=(0xcccccccc&x)>>2+(0x33333333&x), \
x=(0xf0f0f0f0&x)>>4+(0x0f0f0f0f&x), \
x=(0xff00ff00&x)>>8+(0x00ff00ff&x), \
x=x>>16+(0x0000ffff&x))

94. Compute the discrete log of an unsigned integer.

ANS.

#define discrete_log(h) \
(h=(h>>1)|(h>>2), \
h|=(h>>2), \
h|=(h>>4), \
h|=(h>>8), \
h|=(h>>16), \
h=(0xaaaaaaaa&h)>>1+(0x55555555&h), \
h=(0xcccccccc&h)>>2+(0x33333333&h), \
h=(0xf0f0f0f0&h)>>4+(0x0f0f0f0f&h), \
h=(0xff00ff00&h)>>8+(0x00ff00ff&h), \
h=(h>>16)+(0x0000ffff&h))
If I understand it right, log2(2) =1, log2(3)=1, log2(4)=2..... But
this macro does not work out log2(0) which does not exist! How do you
think it should be handled?

* 95. How do we test most simply if an unsigned integer is a power of
two?

ANS. #define power_of_two(x) \ ((x)&&(~(x&(x-1))))

96. Set the highest significant bit of an unsigned integer to zero.

ANS. (from Denis Zabavchik) Set the highest significant bit of an
unsigned integer to zero
#define zero_most_significant(h) \
(h&=(h>>1)|(h>>2), \
h|=(h>>2), \
h|=(h>>4), \
h|=(h>>8), \
h|=(h>>16))

97. Let f(k) = y where k is the y-th number in the increasing sequence
of non-negative integers with the same number of ones in its binary
representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1, f(4)
=
3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k).

Others

98. A character set has 1 and 2 byte characters. One byte characters
have 0 as the first bit. You just keep accumulating the characters in a
buffer. Suppose at some point the user types a backspace, how can you
remove the character efficiently. (Note: You cant store the last
character typed because the user can type in arbitrarily many
backspaces)

99. What is the simples way to check if the sum of two unsigned
integers has resulted in an overflow.

100. How do you represent an n-ary tree? Write a program to print the
nodes of such a tree in breadth first order.

101. Write the 'tr' program of UNIX. Invoked as

tr -str1 -str2. It reads stdin and prints it out to stdout, replacing
every occurance of str1[i] with str2[i].

e.g. tr -abc -xyz
to be and not to be <- input
to ye xnd not to ye <- output
posted @ 2009-12-25 15:46 wxyz2010 阅读(665) | 评论 (0)编辑 收藏

1.OUTPUT
main()
{
int j=32242,k;
k=find(j);
}
int find(int j)
{
if(j>0)
{
j=(j%10)+find(j/10);
printf("%d",j);
}
return j;
}
A: 3 5 7 11 13

2.function add a line and return input string..
what is the problem in this prog.

char* addline(char * s)
{
char buf[1024];
int l=strlen(s);
strcpy(buff,s);
buff[l]='n';
return buff;
}
A: No issue with thr program

3q.
A[M][N] is an array rotated in anti clock wise that is A'[N][M]. then mapping A[i][j]=A'[ _ ][ _ ] in terms of M,N,i,j.

note i,j are 0 indieses
A A'
1 2 3 3 6
4 5 6 2 5
1 4
A: A[i][j]=A'[j*(n-m)] [i*(n-m)]

4Q.we have 2 sorted lists. write a function to merge 2 lists keep it result list sorted. with creating a new node.

node* merge(l1,l2)

struct node{
int a;
node * next;
}

5Q. Write the test cases for the above question

6Q. Write the test cases for the following api
bool fromfiletobuffer(file *ptr, char *buffer, wood sizeofbuf)
posted @ 2009-12-25 15:11 wxyz2010 阅读(166) | 评论 (0)编辑 收藏

Riddles

  • Why is a manhole cover round?
  • How many cars are there in the USA? (A popular variant is "How many gas stations are there in the USA?")
  • How many manhole covers are there in the USA?
  • You've got someone working for you for seven days and a gold bar to pay them. The gold bar is segmented into seven connected pieces. You must give them a piece of gold at the end of every day. If you are only allowed to make two breaks in the gold bar, how do you pay your worker?
  • One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?
  • Imagine a disk spinning like a record player turn table. Half of the disk is black and the other is white. Assume you have an unlimited number of color sensors. How many sensors would you have to place around the disk to determine the direction the disk is spinning? Where would they be placed?
  • Imagine an analog clock set to 12 o'clock. Note that the hour and minute hands overlap. How many times each day do both the hour and minute hands overlap? How would you determine the exact times of the day that this occurs?
  • You have two jars, 50 red marbles and 50 blue marbles. A jar will be picked at random, and then a marble will be picked from the jar. Placing all of the marbles in the jars, how can you maximize the chances of a red marble being picked? What are the exact odds of getting a red marble using your scheme?
  • Pairs of primes separated by a single number are called prime pairs. Examples are 17 and 19. Prove that the number between a prime pair is always divisible by 6 (assuming both numbers in the pair are greater than 6). Now prove that there are no 'prime triples.'
  • There is a room with a door (closed) and three light bulbs. Outside the room there are three switches, connected to the bulbs. You may manipulate the switches as you wish, but once you open the door you can't change them. Identify each switch with its bulb.
  • Suppose you had 8 billiard balls, and one of them was slightly heavier, but the only way to tell was by putting it on a scale against another. What's the fewest number of times you'd have to use the scale to find the heavier ball?
  • Imagine you are standing in front of a mirror, facing it. Raise your left hand. Raise your right hand. Look at your reflection. When you raise your left hand your reflection raises what appears to be his right hand. But when you tilt your head up, your reflection does too, and does not appear to tilt his/her head down. Why is it that the mirror appears to reverse left and right, but not up and down?
  • You have 4 jars of pills. Each pill is a certain weight, except for contaminated pills contained in one jar, where each pill is weight + 1. How could you tell which jar had the contaminated pills in just one measurement?
  • The SF Chronicle has a word game where all the letters are scrambled up and you have to figure out what the word is. Imagine that a scrambled word is 5 characters long:
    1. How many possible solutions are there?
    2. What if we know which 5 letters are being used?
    3. Develop an algorithm to solve the word.
  • There are 4 women who want to cross a bridge. They all begin on the same side. You have 17 minutes to get all of them across to the other side. It is night. There is one flashlight. A maximum of two people can cross at one time. Any party who crosses, either 1 or 2 people, must have the flashlight with them. The flashlight must be walked back and forth, it cannot be thrown, etc. Each woman walks at a different speed. A pair must walk together at the rate of the slower woman's pace.

    Woman 1: 1 minute to cross
    Woman 2: 2 minutes to cross
    Woman 3: 5 minutes to cross
    Woman 4: 10 minutes to cross

    For example if Woman 1 and Woman 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If Woman 4 then returns with the flashlight, a total of 20 minutes have passed and you have failed the mission. What is the order required to get all women across in 17 minutes? Now, what's the other way?

  • If you had an infinite supply of water and a 5 quart and 3 quart pail, how would you measure exactly 4 quarts?
  • You have a bucket of jelly beans. Some are red, some are blue, and some green. With your eyes closed, pick out 2 of a like color. How many do you have to grab to be sure you have 2 of the same?
  • If you have two buckets, one with red paint and the other with blue paint, and you take one cup from the blue bucket and poor it into the red bucket. Then you take one cup from the red bucket and poor it into the blue bucket. Which bucket has the highest ratio between red and blue? Prove it mathematically.

Algorithms

  • What's the difference between a linked list and an array?
  • Implement a linked list. Why did you pick the method you did?
  • Implement an algorithm to sort a linked list. Why did you pick the method you did? Now do it in O(n) time.
  • Describe advantages and disadvantages of the various stock sorting algorithms.
  • Implement an algorithm to reverse a linked list. Now do it without recursion.
  • Implement an algorithm to insert a node into a circular linked list without traversing it.
  • Implement an algorithm to sort an array. Why did you pick the method you did?
  • Implement an algorithm to do wild card string matching.
  • Implement strstr() (or some other string library function).
  • Reverse a string. Optimize for speed. Optimize for space.
  • Reverse the words in a sentence, i.e. "My name is Chris" becomes "Chris is name My." Optimize for speed. Optimize for space.
  • Find a substring. Optimize for speed. Optimize for space.
  • Compare two strings using O(n) time with constant space.
  • Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once. Describe an algorithm to find the repeated number. If you used auxiliary storage in your algorithm, can you find an algorithm that does not require it?
  • Count the number of set bits in a number. Now optimize for speed. Now optimize for size.
  • Multiple by 8 without using multiplication or addition. Now do the same with 7.
  • Add numbers in base n (not any of the popular ones like 10, 16, 8 or 2 -- I hear that Charles Simonyi, the inventor of Hungarian Notation, favors -2 when asking this question).
  • Write routines to read and write a bounded buffer.
  • Write routines to manage a heap using an existing array.
  • Implement an algorithm to take an array and return one with only unique elements in it.
  • Implement an algorithm that takes two strings as input, and returns the intersection of the two, with each letter represented at most once. Now speed it up. Now test it.
  • Implement an algorithm to print out all files below a given root node.
  • Given that you are receiving samples from an instrument at a constant rate, and you have constant storage space, how would you design a storage algorithm that would allow me to get a representative readout of data, no matter when I looked at it? In other words, representative of the behavior of the system to date.
  • How would you find a cycle in a linked list?
  • Give me an algorithm to shuffle a deck of cards, given that the cards are stored in an array of ints.
  • The following asm block performs a common math function, what is it?
    cwd xor ax, dx
    sub ax, dx
  • Imagine this scenario:
    I/O completion ports are communictaions ports which take handles to files, sockets, or any other I/O. When a Read or Write is submitted to them, they cache the data (if necessary), and attempt to take the request to completion. Upon error or completion, they call a user-supplied function to let the users application know that that particular request has completed. They work asynchronously, and can process an unlimited number of simultaneous requests.
    Design the implementation and thread models for I/O completion ports. Remember to take into account multi-processor machines.
  • Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.
  • Write a function to print all of the permutations of a string.
  • Implement malloc.
  • Write a function to print the Fibonacci numbers.
  • Write a function to copy two strings, A and B. The last few bytes of string A overlap the first few bytes of string B.
  • How would you write qsort?
  • How would you print out the data in a binary tree, level by level, starting at the top?

Applications

  • How can computer technology be integrated in an elevator system for a hundred story office building? How do you optimize for availability? How would variation of traffic over a typical work week or floor or time of day affect this?
  • How would you implement copy-protection on a control which can be embedded in a document and duplicated readily via the Internet?
  • Define a user interface for indenting selected text in a Word document. Consider selections ranging from a single sentence up through selections of several pages. Consider selections not currently visible or only partially visible. What are the states of the new UI controls? How will the user know what the controls are for and when to use them?
  • How would you redesign an ATM?
  • Suppose we wanted to run a microwave oven from the computer. What kind of software would you write to do this?
  • What is the difference between an Ethernet Address and an IP address?
  • How would you design a coffee-machine for an automobile.
  • If you could add any feature to Microsoft Word, what would it be?
  • How would you go about building a keyboard for 1-handed users?
  • How would you build an alarm clock for deaf people?

Thinkers

  • How are M&Ms made?
  • If you had a clock with lots of moving mechanical parts, you took it apart piece by piece without keeping track of the method of how it was disassembled, then you put it back together and discovered that 3 important parts were not included; how would you go about reassembling the clock?
  • If you had to learn a new computer language, how would you go about doing it?
  • You have been assigned to design Bill Gates bathroom. Naturally, cost is not a consideration. You may not speak to Bill.
  • What was the hardest question asked of you so far today?
  • If MS told you we were willing to invest $5 million in a start up of your choice, what business would you start? Why?
  • If you could gather all of the computer manufacturers in the world together into one room and then tell them one thing that they would be compelled to do, what would it be?
  • Explain a scenario for testing a salt shaker.
  • If you are going to receive an award in 5 years, what is it for and who is the audience?
  • How would you explain how to use Microsoft Excel to your grandma?
  • Why is it that when you turn on the hot water in any hotel, for example, the hot water comes pouring out almost instantaneously?
  • Why do you want to work at Microsoft?
  • Suppose you go home, enter your house/apartment, hit the light switch, and nothing happens - no light floods the room. What exactly, in order, are the steps you would take in determining what the problem was?
  • Interviewer hands you a black pen and says nothing but "This pen is red."

Chris Sells writes about the set of tester questions his friend got when being interviewed for Software Development Engineer in Test position at Microsoft Corp.

  1. How would you deal with changes being made a week or so before the ship date?
  2. How would you deal with a bug that no one wants to fix? Both the SDE and his lead have said they won’t fix it.
  3. Write a function that counts the number of primes in the range [1-N]. Write the test cases for this function.
  4. Given a MAKEFILE (yeah a makefile), design the data structure that a parser would create and then write code that iterates over that data structure
    executing commands if needed.
  5. Write a function that inserts an integer into a linked list in ascending order. Write the test cases for this function.
  6. Test the save dialog in Notepad. (This was the question I enjoyed the most).
  7. Write the InStr function. Write the test cases for this function.
  8. Write a function that will return the number of days in a month (not using System.DateTime).
  9. You have 3 jars. Each jar has a label on it: white, black, or white&black. You have 3 sets of marbles: white, black, and
    white&black. One set is stored in one jar. The labels on the jars are guaranteed to be incorrect (i.e. white will not contain white). Which jar would you choose from to give you the best chances of identifying the which set of marbles in is in which jar.
  10. Why do you want to work for Microsoft?
  11. Write the test cases for a vending machine. (Those were the questions I was asked. I had a lot of discussions about how to handle situations. Such as a tester is focused on one part of an SDK. During triage it was determined that that portion of the SDK was not on the critical path, and the tester was needed elsewhere. But the tester continued to test that portion because it is his baby. How would you get him to stop testing that portion and work on what needs to be worked on? Other situations came up like arranging tests into the different testing buckets (functional, stress, perf, etc.).)
posted @ 2009-12-25 14:59 wxyz2010 阅读(401) | 评论 (0)编辑 收藏

本地化的问题就是处理不同字符集的问题。多年来,我们一直在将文本字符串编码成一组以
0结尾的单字节字符。许多人对此已经习已为常。调用strlen,它会返回“以0结尾的一个ANSI
单字节字符数组”中的字符数。
 问题是,某些语言和书写系统(例如日本汉字)的字符集有非常多的符号。一个字节最多只
能表示256个符号,因此是远远不够的。为了支持这些语言和书写系统,双字节字符集
(double-byte character set,DBCS)应运而生。在双字节字符集中,一个字符串中的每个字
符都由1个或2个字节组成。以日本汉字为例,如果第一个字符在0x81到0x9F之间,或者在
0xE0到0xFC之间,就必须检查下一个字节,才能判断出一个完整的汉字。对程序员而言,
和双字节字符集打交道如同一场噩梦,因为某些字符是1个字节宽,而有的字符却是2个字节
宽。幸运的是,我们可以把DBCS放到一边,专心利用Windows函数和C运行库对Unicode字
符串的支持。
 
Unicode是1988年由Apple和Xerox共同建立的一项标准。1991年,成立了专门的协会来开发
和推动Unicode。该协会由Apple、 Compaq、Hewlett-Packard、IBM、Microsoft、 Oracle、Silicon
Graphics、Sybase、Unisys和Xerox等多家公司组成(协会成员的最新列表可从
http://www.Unicode.org获得)。该组织负责维护Unicode标准。Unicode的完整描述可以参考
Addison-Wesley出版的The Unicode Standard一书,该书可通过http://www.Unicode.org获得。
 
在Windows Vista中,每个Unicode字符都使用UTF-16编码,UTF的全称是Unicode
Transformation Format( Unicode转换格式)。UTF-16将每个字符编码为2个字节(或者说16
位)。在本书中,我们在谈到Unicode时,除非专门声明,否则一般都是指UTF-16编码。
Windows之所以使用UTF-16,是因为全球各地使用的大部分语言中,每个字符很容易用一
个16位值来表示。这样一来,应用程序很容易遍历字符串并计算出它的长度。但是,16位不
足以表示某些语言的所有字符。对于这些语言,UTF-16支持使用代理(surrogates),后者
是用32位(或者说4个字节)来表示一个字符的一种方式。由于只有少数应用程序需要表示
这些语言中的字符,所以UTF-16在节省空间和简化编码这两个目标之间,提供了一个很好
的折衷。注意,.NET Framework始终使用UTF-16来编码所有字符和字符串,所以在你自己
的Windows应用程序中,如果需要在原生代码(native code)和托管代码(managed code)
之间传递字符或字符串,使用UTF-16能改进性能和减少内存消耗。
 
另外还有其他用于表示字符的UTF标准,具体如下。
¾  UTF-8   UTF-8将一些字符编码为1个字节,一些字符编码为2个字节,一些字符编
码为3个字节,一些字符编码为4个字节。值在0x0080以下的字符压缩为1个字节,
这对美国使用的字符非常适合。0x0080和0x07FF之间的字符转换为2个字节,这对
欧洲和中东地区的语言非常适用。0x0800以上的字符都转换为3个字节,适合东亚
地区的语言。最后,代理对(surrogate pairs)被写为4个字节。UTF-8是一种相当
流行的编码格式。但在对值为0x0800及以上的大量字符进行编码的时候,不如
UTF-16高效。
¾  UTF-32   UTF-32将每个字符都编码为4个字节。如果打算写一个简单的算法来遍
历字符(任何语言中使用的字符),但又不想处理字节数不定的字符,这种编码方
式就非常有用。例如,如果采用UTF-32编码方式,就不需要关心代理(surrogate)
的问题,因为每个字符都是4个字符。显然,从内存使用这个角度来看,UTF-32  并
不是一种高效的编码格式。因此,很少用它将字符串保存到文件或传送到网络。这
种编码格式一般在应用程序内部使用。
 
目前,Unicode为阿拉伯语、汉语拼音、西里尔文(俄语)、希腊语、希伯来语、日语假名、
朝鲜语和拉丁语(英语)字符等——这些字符称为书写符号(scripts)——定义了码位(code point,即一个符号在字符集中的位置)。每个版本的Unicode都在现有的书写符号的基础上
引入了新的字符,甚至会引入新的书写符号,比如腓尼基文(一种古地中海文字)。字符集
中还包含大量标点符号、数学符号、技术符号、箭头、装饰标志、读音符号以及其他字符。

posted @ 2009-12-25 09:53 wxyz2010 阅读(210) | 评论 (0)编辑 收藏