Page

D.8.0- Conversion Functions

Created by Brendan Doss.
Last Updated by Sarah Welna.  

PublicCategorized as Appendix D.

Not yet tagged
<< D.7.0- String constantsAppendixDD.9.0- Unsupported conversion functions >>

Conversion Functions

Normally you don't need to convert values in VBScript, because there is only one data type, the Variant.

 

Implicit conversion is generally applied when needed, but when you pass a value to a non-variant procedure in a COM object that needs the value passed ByRef, you will have to pass the value with the precise data subtype. This can be done by placing the argument in it's own set of parentheses, which forces a temporary evaluation of the argument as an expression:

 

Dim objByRefSample

Dim intTest

' Initialize the variable

intTest = "5"

' Create the object

Set objByRefSample = CreateObject("MyObject.ByRefSample")

' Call the method

objByRefSample.PassIntegerByReference (intTest)
' Destroy the object
Set objByRefSample = Nothing

 

The PassIntegerByReference method is a VB sub-procedure with just one argument of type integer that is passed ByRef.

 

What happens is that the value 5 stored in the intTest variable is actually explicitly coerced into a variable of subtype Integer, so that it conforms to the methods argument type. If you remove the parentheses, you will get a runtime error, because the implicit coercion will treat the string value as a double.

 

This is just one way of solving the problem. Another way is to use the CInt conversion function (listed below) when calling the method.

 

At some point however, you might need to convert a value of one data subtype to another data subtype. This can be necessary for various reasons:

 

  • You need to present a number in hexadecimal notation instead of decimal
  • You need the corresponding character code for a character or vice versa
  • You need to pass values to a non-variant property procedure or as a function parameter in a COM object
  • You need to save data in a database

 

See Chapter 2 Variables and Data Types for an explanation of the different data types.

 

Asc

Returns the ANSI character code for the first character in a string.

 

Syntax

 

Asc(string)

 

string is any valid string expression.

Note

 

A runtime error occurs if string doesn't contain any characters. string is converted to a String subtype if it's a numeric subtype.

 

 

Example

intCharCode = Asc("WROX")

 

intCharCode now holds the value 87, which is the ANSI
character code for "W".

 

See Also

AscB, AscW, Chr, ChrB and ChrW

 

AscB

Returns the ANSI character code for the first byte in a string containing byte data.

 

Syntax

 

AscB(string)

string is any valid string expression.

 

Note

 

A runtime error occurs if string doesn't contain any characters. For normal ANSI strings this function will return the same as the Asc function. Only if the string is in Unicode format will it be different from Asc. Unicode characters are represented by two bytes as opposed to ANSI characters that only need one.

 

Example

intCharCode = AscB("WROX")

 

intCharCode now holds the value 87, which is the ANSI
character code for "W".

 

See Also

Asc, AscW, Chr, ChrB and ChrW

 

AscW

Returns the Unicode character code for the first character in a string.

 

Syntax

 

AscW(string)

string is any valid string expression.

 

Note

 

A runtime error occurs if string doesn't contain any characters. string is converted to a String subtype if it's a numeric subtype. For use on 32-bit Unicode enabled platforms only, to avoid conversion from Unicode to ANSI.

 

Example

intCharCode = AscW("WROX")

 

intCharCode now holds the value 87, which is the Unicode character code for "W".

 

See Also

Asc, AscB, Chr, ChrB and ChrW

 

CBool

Returns a Boolean value (Variant subtype 11) corresponding to the value of an expression.

 

Syntax

 

CBool(expression)

expression is any valid expression.

 

Note

 

A runtime error occurs if expression can't be evaluated to a numeric value.

If expression evaluates to zero then false is returned; otherwise, true is returned.

 

Example

Dim intCounter, blnValue
intCounter = 5

blnValue = CBool(intCounter)

 

blnValue now holds the value true, because intCounter holds a non-zero value.

 

See Also

CByte, CCur, CDbl, CInt, CLng, CSng and CStr

 

CByte

Returns an expression converted to Variant subtype Byte (17).

 

Syntax

 

CByte(expression)

expression is any valid numeric expression.

 

Note

 

A runtime error occurs if expression can't be evaluated to a numeric value or if expression evaluates to a value outside the acceptable range for a Byte (0-255). Fractional values are rounded.

 

Example

Dim dblValue, bytValue

dblValue = 5.456

bytValue = CByte(dblValue)

 

bytValue now holds the value 5, because dblValue is rounded.

 

See Also

CBool, CCur, CDbl, CInt, CLng, CSng and CStr

 

CCur

Returns an expression converted to Variant subtype Currency (6).

 

Syntax

 

CCur(expression)

expression is any valid expression.

 

Note

 

CCur is internationally aware, which means that the return value is based on the locale settings on the machine. Numbers will be formatted with the appropriate decimal separator and the fourth digit to the right of the separator is rounded up if the fifth digit is 5 or higher.

 

Example

Dim dblValue, curValue

dblValue = 724.555789

curValue = CCur(dblValue)

 

curValue now holds the value 724.5558 or 724,5558, depending on the separator.

 

See Also

CBool, CByte, CDbl, CInt, CLng, CSng and CStr

 

CDate

See under Date & Time functions

 

CDbl

Returns an expression converted to Variant subtype Double (5).

 

Syntax

 

CDbl(expression)

 

expression is any valid expression.

 

Note

 

CDbl is internationally aware, which means that the return value is based on the locale settings on the machine. Numbers will be formatted with the appropriate decimal separator. A runtime error occurs if expression lies outside the range (-1.79769313486232E308 to -4.94065645841247E-324 for negative values, and 4.94065645841247E-324 to 1.79769313486232E308 for positive values) applicable to a Double.

 

Example

Dim dblValue

dblValue = CDbl("5,579.56")

 

dblValue now holds the value 5579.56 or 5,57956, depending on
the thousand and decimal separators in use.

 

See Also

CBool, CByte, CCur, CInt, CLng, CSng and CStr

 

Chr

Returns the ANSI character corresponding to charactercode.

 

Syntax

 

Chr(charactercode)

charactercode is a numeric value that indicates the character you want.

 

Note

 

Supplying a charactercode from 0 to 31 will return a standard non-printable ASCII character.

 

Example

Dim strChar

strChar = Chr(89)

 

strChar now holds the character Y which is number 89 in the ANSI character table.

 

See Also

Asc, AscB, AscW, ChrB and ChrW

 

ChrB

Returns the ANSI character corresponding to charactercode.

 

Syntax

 

ChrB(charactercode)

charactercode is a numeric value that indicates the character you want.

 

Note

 

Supplying a charactercode from 0 to 31 will return a standard non-printable ASCII character. This function is used instead of the Chr (returns a two-byte character) function when you only want the first byte of the character returned.

 

Example

Dim strChar

strChar = ChrB(89)

 

strChar now holds the character Y which is number 89 in the ANSI character table.

 

See Also

Asc, AscB, AscW, Chr and ChrW

 

ChrW

Returns the Unicode character corresponding to charactercode.

 

Syntax

 

ChrW(charactercode)

 

charactercode is a numeric value that indicates the character you want.

 

Note

 

Supplying a charactercode from 0 to 31 will return a standard non-printable ASCII character. This function is used instead of the Chr function when you want to return a double byte character. For use on 32-bit Unicode enabled platforms only, to avoid conversion
from Unicode to ANSI.

 

Example

Dim strChar

strChar = ChrW(89)

 

strChar now holds the character Y which is number 89 in the Unicode character table.

 

See Also

Asc, AscB, AscW, Chr and ChrB

 

CInt

Returns an expression converted to Variant subtype Integer (2).

 

Syntax

 

CInt(expression)

expression is any valid expression.

 

Note

 

CInt is internationally aware, which means that the return value is based on the locale settings on the machine. Please note that decimal values are rounded, before the fractional part is discarded. A runtime error occurs if expression lies outside the range (-32,768 to 32,767) applicable to an Integer.

 

Example

Dim intValue

intValue = CInt("5,579.56")

 

intValue now holds the value 5580 or 6, depending on the thousand and decimal separators in use.

 

See Also

CBool, CByte, CCur, CDbl, CLng, CSng, CStr and the Math Functions Fix and Int

 

CLng

Returns an expression converted to Variant subtype Long (3).

 

Syntax

 

CLng(expression)

expression is any valid expression.

 

Note

 

CLng is internationally aware, which means that the return value is based on the locale settings on the machine. Please note that decimal values are rounded, before the fractional part is discarded. A runtime error occurs if expression lies outside the range (-2,147,483,648 to 2,147,483,647) applicable to a Long.

 

Example

Dim lngValue

lngValue = CLng("5,579.56")

 

lngValue now holds the value 5580 or 6, depending on the thousand and decimal separators in use.

 

See Also

CBool, CByte, CCur, CDbl, CInt, CSng, CStr, and the Math Functions Fix and Int

 

CSng

Returns an expression converted to Variant subtype Single (4).

 

Syntax

 

CSng(expression)

expression is any valid expression.

 

Note

 

CSng is internationally aware, which means that the return value is based on the locale settings on the machine. A runtime error occurs
if expression lies outside the range (-3.402823E38 to -1.401298E-45 for negative values, and 1.401298E-45 to 3.402823E38 for positive values) applicable to a Single.

 

Example

Dim sngValue

sngValue = CSng("5,579.56")

 

sngValue now holds the value 5579.56 or 5,57956, depending on the thousand and decimal separators in use.

 

See Also

CBool, CByte, CCur, CDbl, CInt, CLng, CStr and the Math Functions
Fix and Int

 

CStr

Returns an expression converted to Variant subtype String (8).

 

Syntax

 

CStr(expression)

expression is any valid expression.

 

Note

 

CStr is internationally aware, which means that the return value is based on the locale settings on the machine. A runtime error occurs if expression is Null. Numeric and Err values are returned as numbers, Boolean values as true or false, and Date values as a short date.

 

Example

Dim strValue

strValue = CStr("5,579.56")

 

strValue now holds the value 5,579.56.

 

See Also

CBool, CByte, CCur, CDbl, CInt, CLng, CSng and the Math Functions Fix and Int

 

Fix

See under Math functions

 

Hex

Returns the hexadecimal representation (up to 8 characters) of a number as a Variant subtype String (8).

 

Syntax

 

Hex(number)

number is any valid expression.

 

Note

 

number is rounded to nearest even number before it is evaluated. Null will be returned if number is Null.

 

Example

Dim strValue

strValue = Hex(5579.56)

 

strValue now holds the value 15CC.

 

See Also

Oct

 

Int

See under Math functions

 

Oct

Returns the octal representation (up to 11 characters) of a number
as a Variant subtype String (8).

 

Syntax

 

Oct(number)

expression is any valid expression.

 

Note

 

number is rounded to nearest whole number before it is evaluated. Null will be returned if number is Null.

 

Example

Dim strValue

strValue = Oct(5579.56)

 

strValue now holds the value 12714.

 

See Also

Hex

 

<< D.7.0- String constantsAppendixDD.9.0- Unsupported conversion functions >>