Re: How to read Unicode(Big-Endian) text file(s) in Non-MFC
"Giovanni Dicanio" <giovanni.dicanio@invalid.com> wrote in message
news:OrIwpjicIHA.4016@TK2MSFTNGP03.phx.gbl...
"meme" <meme@myself.com> ha scritto nel messaggio
news:eJlvmRicIHA.4844@TK2MSFTNGP04.phx.gbl...
I'm trying to read unicode text files.... so far I'm able to do
following....but lost in "Big-Endian" thingies...
Reading MSDN documentation about fopen, it seems that it can handle
Unicode UTF-16 LE, but not BE.
http://msdn2.microsoft.com/en-us/library/yeby3zcb.aspx
So, I think you should just read the raw WORDs (16 bits, two bytes) from
file, and swap the byte order from your code.
1. For each WORD in file
2. read that WORD
3. swap low-byte and high-byte, transforming the WORD from BE to LE
4. store this LE word (Unicode UTF-16LE wchar_t) in memory
To swap two bytes in a word, you may use the following code:
<code>
// Converts a word from Big-Endian to Little-Endian (or vice-versa)
inline WORD SwapWordEndiannes(WORD w)
{
// Swap low and high bytes
return MAKEWORD( HIBYTE(w), LOBYTE(w) );
}
WORD bigEndianWord = ...;
WORD littleEndianWord = SwapWordEndiannes(bigEndianWord);
</code>
HTH,
Giovanni
Hi.... thanks for responding.....
so I tried ......following.....but I think I missed or messed up something
and therefore all I see some junk characters when executed ..... :(
// Converts a word from Big-Endian to Little-Endian (or vice-versa)
inline WORD SwapWordEndiannes(WORD w)
{
// Swap low and high bytes
return MAKEWORD( HIBYTE(w), LOBYTE(w) );
}
WORD GetBigWord(FILE *FilePtr)
{
register WORD word;
word = (WORD) (fgetc(FilePtr) & 0xff);
word = ((WORD) (fgetc(FilePtr) & 0xff)) | (word << 0x08);
return(word);
}
void readUnicodeBE(FILE *file)
{
long flen;
flen = _filelength(_fileno(file));
wchar_t *data = new wchar_t[flen + 1];
// rewind(file);
WORD bigEndianWord;
WORD littleEndianWord;
int i=0;
fseek(file, 2, SEEK_SET);
while(!feof(file))
{
bigEndianWord = GetBigWord(file);
littleEndianWord = SwapWordEndiannes(bigEndianWord);
data[i] = (wchar_t)littleEndianWord;
i++;
}
fclose(file);
data[i] = '\0';
SetWindowText(hwndEdit, data);
delete []data;
}