473,437 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,437 software developers and data experts.

Unicode and VBA

Hello All,

I am trying to read Unicode (Kanji character) data from a text file.
When I store unicode data into variable my Kanji character gets messed
up.

I am using following code

Open File1 For Input Access Read As #1
While Not EOF(1)
Line Input #1, data ' each line has English + Kanji characters
data
'Debug.Print data

Can some one help or guide to store English + Kanji (Unicode) data.

Thank you,
AVR

Nov 19 '05 #1
1 17491
On 18 Nov 2005 15:39:54 -0800, an********@yahoo.com wrote:
Hello All,

I am trying to read Unicode (Kanji character) data from a text file.
When I store unicode data into variable my Kanji character gets messed
up.

I am using following code

Open File1 For Input Access Read As #1
While Not EOF(1)
Line Input #1, data ' each line has English + Kanji characters
data
'Debug.Print data

Can some one help or guide to store English + Kanji (Unicode) data.

Thank you,
AVR


What are you going to do with the codes? Doing anything with unicode in vba is hard work.
Here is one way of reading a unicode text file into a byte array. Each unicode char is a pair of
bytes, low order byte first.

For inf on FSO see http://www.aivosto.com/visdev/fso.html

For inf on how VB stores strings see
http://msdn.microsoft.com/library/de...og6_topic1.asp
from which the function BSTRtoLPWSTR is taken.

Option Compare Database
Option Explicit

Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (lpDest As Any, _
lpSource As Any, ByVal cbCopy As Long)

Public Sub aaa()
'read unicode file into byte array b

Dim myFSO As New Scripting.FileSystemObject
Dim ts As TextStream
Dim alltext As String
Dim b() As Byte
Dim lpsz As Long, lng As Long

Set ts = myFSO.OpenTextFile("c:\Hindi.txt", ForReading, , TristateTrue)
'TriStateTrue opens the file as Unicode.
alltext = ts.ReadAll

lng = BSTRtoLPWSTR(alltext, b, lpsz)
'lng is number of bytes

'now do what you need to do with the bytes

End Sub
Function BSTRtoLPWSTR(sBSTR As String, b() As Byte, lpwsz As Long) As Long

' Input: a nonempty BSTR string
' Input: **undimensioned** byte array b()
' Output: Fills byte array b() with Unicode char string from sBSTR
' Output: Fills lpwsz with a pointer to b() array
' Returns byte count, not including terminating 2-byte Unicode null character
' Original BSTR is not affected

Dim cBytes As Long

cBytes = LenB(sBSTR)

' ReDim array, with space for terminating null
ReDim b(1 To cBytes + 2) As Byte

' Point to BSTR char array
lpwsz = StrPtr(sBSTR)

' Copy the array
CopyMemory b(1), ByVal lpwsz, cBytes + 2

' Point lpsz to new array
lpwsz = VarPtr(b(1))

' Return byte count
BSTRtoLPWSTR = cBytes

End Function

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
8
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
6
by: Jeff | last post by:
Hi - I'm setting up a streamreader in a VB.NET app to read a text file and display its contents in a multiline textbox. If I set it up with System.Text.Encoding.Unicode, it reads a unicode...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.