VB6 built-in internal code conversion function

Source: Internet
Author: User
Tags integer

Hey, see this topic, must be scared a big jump bar, or put on a pair of not believe expression? Don't worry, please listen to me slowly.

The VB6, it's outrageous, is built to support Unicode, which means that when you read a string, it converts it to Unicode based on the local default language's corresponding relationship, and then converts it from Unicode to local encoding. On the one hand, it is of course convenient, any language version of what is convenient, and in the Unicode system, such as NT, it can be a duck, Unicode is after all the direction of software development, such as the Chinese Win9x used in the GBK code, is actually a big step toward Unicode. On the other hand, this support actually greatly slows down the processing time of strings! Think about it, if we read a line from a file and display it, it would have been very simple, but in VB6 there was actually a bit of time to convert to Unicode, if we read a line from the original file with a string and then parse , and then write to the destination file, in fact, more than a dual time (local to Unicode,unicode to local). Of course, for such a trivial matter, experts must also have to deal with the method (such as using a byte array instead of strings)

Said so a lot of Unicode, not to the point, because the VB6 built-in Unicode support, and Unicode inside and GBK and Big5 characters have a corresponding relationship, so to achieve the conversion is just a small matter, the following we will be invited to the leading actress appearances. (Hua Hua hua hua ...) Applause. )

function strconv! This meimei everyone may have seen in the VB5, she has some bodyguards, can help her string in Unicode and local encoding between the conversion, and in the VB6 inside, StrConv again a bodyguard! Please look carefully:

StrConv (string,conversion) ' VB5

StrConv (string,conversion,lcid) ' VB6

String, which is a pre-converted string (a hint or a byte array).

Conversion: is an integer, only to determine the conversion mode, VB definition of some constants, such as vbFromUnicode, Vbunicode and so on, these need not I explain that we should also understand it.

LCID: Ha, this is VB6 new parameter, long integer, you can specify the encoding method, such as &h404, that is, Big5 code, &h804 that GBK code, reasonable use of this parameter, you can write a very simple inside code conversion tool!

What the? You do not understand, ah, I will give you the source code to see it.

Dim iReadNumber As Integer `读文件号
Dim iWriteNumber As Integer `写文件号
Dim mem() As Byte `byte数组
Dim strFileName As String `文件名
Dim lLength As Long `文件长度
`将文件内容读入mem byte数组
iReadNumber = FreeFile
strFileName = TextFileName.Text
lLength = FileLen(strFileName)
ReDim mem(lLength) As Byte
Open strFileName For Binary As #iReadNumber
Get #iReadNumber,,mem
Close #iReadNumber
`将mem数组转换为Big5码所对应的Unicode码
mem = StrConv(mem,vbUnicode,&H404)
`再将Unicode码转换为GBK编码
mem = StrConv(mem,vbFromUnicode,&H804)
`写到源文件里去
iWriteNumber = FreeFile
Kill strFileName
Open strFileName For Binary As #iWriteNumber
Put #iWriteNumber,,mem
Close #iWriteNumber

' End

(the author uses the Chinese VB6 Professional Edition + Service Pack3).

How is it, convenient? Just a few lines to the BIG5 code into the GBK.

But convenient is convenient, but, also has its own flaw, honestly, use this method to convert GB\GBK to Big5 code is very good, but the Big5 code turned over, the result is actually traditional! Looks really uncomfortable, eh, there's no way, Unicode inside is the corresponding relationship, in addition, such conversion is after all walk two steps, so, theoretically speaking, than other transcoding software is one times slower!

But there are two great benefits, one, there will be no wrong code, this is international standards yo, what do you think? Second, there will be no shortage of words, like many transcoding software is now from BIG5 to GB code, to see 15000 words turn to six thousand or seven thousand words, one by one mapping, no lack of words to blame!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.