Conversion functions of various data types in VB

Source: Internet
Author: User
Tags types of tables

In VB, many data type conversion functions are provided, especially the conversion of various hexadecimal data and conversion functions of strings and numbers. This article will make a simple summary of these functions.

1. Various hexadecimal conversion functions
Generally, the hexadecimal format includes decimal, binary, and hexadecimal. decimal is the data base we use in our daily life, so we are most familiar with and clear. Binary is the basic computer base, mainly because it is easy to use physical devices to implement binary; octal and hexadecimal systems are commonly used in computer because of their natural intimacy with binary systems, which is more convenient than binary systems.

The conversion functions of the data in VB include the following:
Hex function: returns a string representing a hexadecimal value;
Oct function: returns the variant (string) representing the octal value of a value );
CINT function: forces an expression to be converted to an integer from-32,768 to 32,767;
Clng function: forcibly converts an expression to a long integer from-2,147,483,648 to 2,147,483,647;
Cdec function: converts an expression to a decimal data type;
Cdbl function: forces an expression to be converted to the double data type;
 

These functions are the most basic conversion functions between several data hexadecimal functions. The previous three functions are the most commonly used and useful. from the above functions, we can see that VB does not provide us with special functions for us to convert expressions or data into decimal and binary, however, we can use the CINT function to easily convert the decimal type, while the binary implementation can be easily converted through the binary, octal, and hexadecimal relations.
 

The following are examples of hex, Oct, and CINT functions:
(1). A = hex (5) returns 5;
(2). B = hex (10) returns
(3). c = hex (23) returns 17
(4). d = OCT (5) returns 5
(5). e = OCT (10) returns 12
(6). F = OCT (23) returns 27
(7). G = CINT (& H17) returns 23
(8). H = CINT (& o12) returns 10
 

The application of these functions is relatively simple and I will not talk about it. It is worth mentioning that both Hex and OCT functions return strings, if you want to convert the string variables in hexadecimal or octal to decimal, you can do the following:
C = "17" 17 is the string of the hexadecimal value
C = "& H" & C
Ic = CINT (c) returns 23

2. String and numeric conversion functions
In VB, there are many string functions, which are also convenient, so we will not introduce them one by one. This article mainly focuses on
The conversion functions are summarized as follows:
STR () and Val () are used to convert strings and numbers;
CHR () and ASC () are used to convert strings and ASCII codes;
Chrw () and ASCW () are used for conversion between Unicode codes and Chinese characters;
Format () is a function that is widely used and has powerful functions.
 

In these functions, the first two pairs and the format () functions are frequently used. Here we only provide a few simple examples of the first two pairs:
(1) mystring = STR (-459.65) 'returns "-459.65 ".
Mystring = STR (459.001) 'returns "459.001 ".
(2) myvalue = Val ("2 45 7") 'returns 2457.
Myvalue = Val ("24 and 57") 'returns 24.
(3) mychar = CHR (97) 'returns.
Mychar = CHR (37) 'returns %.
(4) mynumber = ASC ("apple") 'returns 65.
Mynumber = ASC ("A") 'returns 97.

The format function is very powerful. You can refer to the msdn instructions. this article focuses on the third pair of functions, which are often overlooked. when I was developing a mobile phone text message, I first developed it in the text mode. Later, due to the change of Chinese characters to the PDU mode, the PDU mode can be used in three forms: 7-bits, 8-bits, ucs2. the first two types of tables are complex and involve the design of encoding functions. I will not talk about them here. I use ucs2 encoding, it is found that only the third pair of functions can complete the PDU string encoding and decoding. the usage of the function is exactly the same as that of the second pair. The following example showsCodeFrom the immediate window of VB ):
? ASCW ("medium ")
20013
? ASCW ("Fruit ")
26524
? ASCW ("e ")
101
? Chrw (101)
E
? Chrw (26589)
Bytes
In the chrw () and ASCW () functions, ASCII is still treated as CHR () and ASC. the above is just a summary of the use of these functions, hoping to play a role.

 

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.