Saturday, July 18, 2009

Single Byte Characters and Multi Byte Characters

When we were trying to implement Recursive File reading program in Windows, we were not knowing the concept of Multibyte characters or Wide character. With the help of google search, we were putting 'L' infront of strings. Like
char *ptr= L"Pramod";

We didnt see what the 'L' will do, at the first time. win32 API were executing in downloaded codes. But when we started to do our own implementations, we were using ANSI APIs also in our code. All ANSI C APis were failing!

We have printed the string which is attached with 'L', using for loop. NULL was printing alternatively!

Then we came to know the job of 'L' and TEXT("pramod"); These will put the NULL between alternative characters in the string and make it 'Multibyte characters'.

If i do like, char *ptr="Pramod", in memory it will be 'p0r0a0m0o0d0a00'. Zero will be the alternative character.

Most of ANSI C APIs requires single byte characters. Hence we have written 2 functions for converting Multibyte to Single byte and, Singlebyte to Multibyte.

int ConvertToSingleBytes(char *destpath, char *path)
{
int k,temp1,i;
k=0;
temp1 = lstrlen(path);
i=0;
while(temp1)
{
destpath[i] = path[k];
i++;
k+=2;
temp1--;
}
destpath[i] = '\0';
return 1;
}

Function for Converting SingleByte to MultiByte

int ConvertBackToWide(char*temp_path, char *path)
{
int i, j, h;
h = strlen(path);
i=0; j=0;
while(i < h)
{
temp_path[j]=path[i];
temp_path[j+1] = '\0';
i++; j+=2;
}
temp_path[j] = '\0';
temp_path[j+1] = '\0';
return 0;
}

Also, MultiByte character are called as Wide Characters. And, some ANSI C APIs also has come under wide characters.



No comments:

Post a Comment