Meanings and uses of vbunicode and vbfromunicode of strconv Functions

Source: Internet
Author: User
Tags base64 to text

Iamlaosong

This section describes the meanings and usage of the last two values of the strconv function conversion, and provides examples.

1. Syntax

Strconv (string, conversion, lcid) the syntax of the strconv function includes the following naming parameters: some parameters are required for string. The string expression to be converted. Required conversion parameters. Integer. The sum of its values determines the type of conversion. Lcid is optional. If it is different from the system localeid, It is localeid (the default value is the system localeid .) The conversion parameter is set to a constant value, indicating that vbuppercase 1 converts string text to uppercase. Vblowercase 2 converts string text to lowercase. Vbpropercase 3 converts the start letter of each character in the string into uppercase letters. Vbwide * 4 * converts single-byte characters in a string into double-byte characters. Vbnarrow * 8 * converts double-byte characters in a string into single-byte characters. Vbkatakana ** 16 ** converts a hirakana character in a string into a katakana character. Vbhiragana ** 32 ** converts a katakana character in a string into a hirakana character. Vbunicode 64 converts a string to Unicode based on the system's default code page. Vbfromunicode 128 converts a string from Unicode to the default code page of the system. * Apply to the far east country. ** Only applicable to Japan. Note: The first three parameters are relatively simple. If we cannot use the last four parameters, we will not talk about them. We will focus on the next two parameters.

2. ANSI format

In the syntax, the default code page is the ANSI mode. In the English environment, the ANSI format is actually the ASCII code. In other environments, it is different. For example, the Chinese environment is ASCII, one byte represents a character, while gb2312 and two byte represents a Chinese character. Therefore, the ANSI format in the Chinese environment is ASCII code + gb2312. In earlier dos systems, plain text is in this format, in this format, the highest bit is used to determine whether it is a Chinese character (the highest bit is 1) or an ASCII character (the highest bit is 0 ). Text files are generally saved in ANSI format in Chinese environments, but there are other formats, such as UTF-8.

3. uncode Encoding

Unicode (unified code, universal code, Single Code) is a character encoding used on a computer. Unicode is generated to address the limitations of traditional character encoding schemes. It sets a uniform and unique binary encoding for each character in each language, to meet the requirements of cross-language and cross-platform text conversion and processing. Unicode represents a character in two bytes, covering all the characters in the world. It is incompatible with the previous character set. The internal string of VB is unicode encoded, so when we open a text file and read data, the content in its memory is different from that in the text file. After conversion, unless you read data in binary format.

4. vbunicode and vbfromunicode

As mentioned above, the meanings of these two parameters are easy to understand, that is, the interchange between Unicode encoding and ANSI encoding. For example:

Textline = strconv (plaintext, vbunicode)

Here, textline is a string variable, and plaintext stores the characters in ANSI mode as a Byte variable. When VB opens a file and reads the text, the above conversion is actually automatically performed.

Plaintext = strconv (textline, vbfromunicode)

The opposite conversion is to convert the Unicode string to the ANSI mode. The conversion result must be saved in bytes.

5. vbunicode and vbfromunicode usage

Because the content in the memory is inconsistent with the content in the file, this type of conversion must be used, especially for data exchange, data encryption, and decryption between systems. If conversion is not performed, the content is inconsistent.

For example, we encrypt a text file, which is stored in ANSI format. When reading a row from the file content to the memory, the content is automatically converted to unicode format, if encryption is performed on the file at this time, the result is different from the string encryption result in the file. Such a result cannot be decrypted by others. If you convert the content that is read into the memory (textline is read ):

Plaintext = strconv (textline, vbfromunicode)


Then encrypt plaintext, and the result will be the same.

Example:

Status = "Encrypting File" Open filename for input as #1 'Open the input file. Open filename2 for output as #2 'Open the output file. Do while not EOF (1) line Input #1, textline plaintext = strconv (textline, vbfromunicode) status = "encrypting data" m_rijndael.setcipherkey pass, keybits implements plaintext, ciphertext, 0 status = "converting text to base64" textline = base64encode (ciphertext) status = "" Print #2, textline' writes the string to the file. Loop close


Status = "decrypting file" Open filename for input as #1 'Open the input file. Open filename2 for output as #2 'Open the output file. Do while not EOF (1) line Input #1, textline status = "converting base64 to text" ciphertext = base64decode (textline) status = "decrypting data" m_rijndael.setcipherkey pass, keybits if m_rijndael.arraydecrypt (plaintext, ciphertext, 0) <> 0 then status = "" Exit sub end if textline = strconv (plaintext, vbunicode) for I = 0 to ubound (plaintext) debug. print plaintext (I) Next I k = instr (1, textline, CHR (0), vbbinarycompare) IF k> 0 then textline = left (textline, k-1) 'intercept the encrypted 0 msgbox textline & "end" status = "" Print #2, textline' to write the string to the file. Loop close





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.