Hi ,
I need a help in handling double byte characters( Japanese, Chinese kanji characters) and writing into an Ascii file.
My requirement is to write data into the file in fixed positions., Eg. col 1-40 will have one value, 41-72 will have another value and so on. The column positions are fixed for each value. If the value doesn't occupy the entire column width, then it has to be filled with blank spaces. For eg., if the col1 value is "Hello" then the remaining 35 positions will be filled with blank spaces.
Now suppose I write a Chinese/japanese character, then every character being a double byte character, it is supposed to occupy 2 positions. Suppose a string has 5 double bytes, then it is supposed to occupy 10 column positions in ascii file. Now because the col1 width is 40 , now I'm supposed to add only 30 blank spaces, otherwise it'll spill over into the next column.
( If the file opened with a proper code page , then it will show the actual characters, but if it is opened with a English code page, then you'll actually see these characters occupying 10 positions but all of them appear as junk.)
So in my
VB code, I'm currently doing this:
First convert string into byte array,
Loop over byte array, and check if the character is a double byte or not.
(Because
VB stores all characters as unicode, byte array would occupy the double the size of a string.
So if the character is a double byte, then both b(0) and b(1) to have value greater than 0.
If the character is a Ascii, then b(0) will have value greater than 0, but b(1) will be 0.
I checked the AscW value for these characters, some are greater than 255 and some have negative values also.)
b = myStr
For i = 0 To UBound(b) Step 2
If b(i + 1) <> 0 Then 'Implies double byte character
NoOfDoubleByte = NoOfDoubleByte + 1
End If
Next
then based on the no. of double bytes, reconstruct the string with appropriate no. of spaces.
Suppose the string is supposed to occupy positions 1-40, then decrement the No of double bytes in the string and add that many spaces.
mystr = mystr & space$(40-(len(mystr + NoOfDoubleByte ))
Then finally write this into an ascii file.
Open FileName For Append As #intFile
Print #intFile, myStr
Now the problem.
The logic appeared to work fine so far. But suddenly I find that there are some Japanese characters, though being double byte is appeared to occupy only one position,. ( When opened in note pad with a English code page , a few charaters have occupied 2 positions , but a few of them have occupied only one position.)
so this entire logic of building string has gone haywire. Can some one direct me to the correct way of handling this.
Any help is highly appreciated.
Regards
Rajesh