Conversion Functions

VBScript Conversion Functions

1) Asc Function

Description: Returns the ANSI character code corresponding to the first letter in a string.

‘ANSI Ranges: ‘A-Z (65 to 90), ‘(a-z) (97 to 122), ‘(0-9) (48 to 57)

Dim x
x = “GCREDDY”
Msgbox Asc(x) ’71
Msgbox Asc(“A”) ’65
Msgbox Asc(“ABC”) ’65
Msgbox Asc(“Z”) ’90
Msgbox Asc(“a”) ’97
Msgbox Asc(“z”) ‘122
Msgbox Asc(“*”) ’42
Msgbox Asc(1) ’49

2) Chr Function

Description: Returns the character associated with the specified ANSI character code.

Example:
Msgbox Chr(65) ‘A
Msgbox Chr(90) ‘Z
Msgbox Chr(97) ‘a
Msgbox Chr(122) ‘z
Msgbox Chr(49) ‘1
Msgbox Chr(42) ‘*

3) CBool Function

Description: Returns an expression that has been converted to a Variant of subtype Boolean.

Example:

Dim X, Y, Check
X= 10
Y = 10
Check = CBool(X = Y)
Msgbox Check ‘Returns True

X = 5
Check = CBool(X = Y)
Msgbox Check ‘Returns False

4) CByte Function

Description: Returns an expression that has been converted to a Variant of subtype Byte.

Example:

Dim MyDouble, MyByte
MyDouble = 100.5678        ‘ It is a Double.
MyByte = CByte(MyDouble)
Msgbox MyByte ‘ MyByte contains 101.

MyDouble = 100.4678        ‘ It is a Double.
MyByte = CByte(MyDouble)
Msgbox MyByte ‘ MyByte contains 100.

5) Cint Function

Description: Returns an expression that has been converted to a Variant of subtype Integer.

Example:

Dim a
a=”100″
Msgbox VarType(a) ‘8 for String

a=CInt(a)
Msgbox VarType(a) ‘2 for Integer

6) CDbl Function

Description: Returns an expression that has been converted to a Variant of subtype Double.

Example:

Dim b

b= “100.45”
Msgbox VarType(b) ‘8 for String

b=CDbl(b)
Msgbox VarType(b) ‘5 for Double

7) CStr Function

Description: Returns an expression that has been converted to a Variant of subtype String.

Example:

Dim Val1, Val2, MyString
Val1 = 123.324         ‘ It is a Double value.

Msgbox VarType(Val1) ‘ 5 for Double

MyString = CStr(Val1)   ‘ MyString contains “123.324”

Msgbox VarType(MyString) ‘ 8 for String

8) CSng Function

Description: Returns an expression that has been converted to a Variant of subtype Single.

Example:

Dim a, b, c, d
a = 10.12345678: b = 20.98765432 20.98765432
c = CSng(a)
d = CSng(b)

Msgbox c ‘10.12345
Msgbox d ‘20.98765

9) CLng Function

Description: Returns an expression that has been converted to a Variant of subtype Long.

Example:
Dim a, b, c, d
a = 123.45: b = 123.55
c = CLng(a)
d = CLng(b)

Msgbox c ‘123
Msgbox d ‘124

0