Export (0) Print
Expand All

Char to Decimal Conversion

.NET Framework 1.1

Converts a Unicode character to a Decimal.

[Visual Basic]
returnValue = Decimal.op_Implicit(value)
[C#]
public static implicit operator decimal(
 char value
);
[C++]
public: static Decimal op_Implicit(
 __wchar_t value
);
[JScript]
returnValue = value;

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own.

[JScript] In JScript, you can use the conversion operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

value
A Unicode character.

Parameters [C#, C++]

value
A Unicode character.

Return Value

A Decimal that represents the converted Unicode character.

Example

[Visual Basic, C#, C++] The following code example converts Char (Unicode characters) to Decimal numbers using the Char to Decimal conversion. This conversion is implicit in C#, but requires the op_Implicit operator in Visual Basic and C++. Implicit conversions to Decimal use other methods in these languages.

[Visual Basic] 
' Example of the op_Implicit conversion from Char to Decimal.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module DecimalFromCharDemo

    Const formatter As String = _
        "{0,6}{1,15}{2,10:X8}{3,9:X8}{4,9:X8}{5,9:X8}"
 
    ' Convert the Char argument and display the Decimal value.
    Sub DecimalFromChar( argument As Char )

        Dim decValue    As Decimal
        Dim bits( )     As Integer
          
        ' The compiler invokes a constructor in Visual Basic 
        ' unless op_Implicit is explicitly called.
        decValue = Decimal.op_Implicit( argument )

        ' Display the Decimal and its binary representation.
        bits = Decimal.GetBits( decValue )
        Console.WriteLine( formatter, argument, decValue, _
            bits( 3 ), bits( 2 ), bits( 1 ), bits( 0 ) )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the op_Implicit conversion from Char " & _
            "to Decimal generates the " & vbCrLf & "following " & _
            "output. It displays the Decimal value and its " & _
            "binary representation." & vbCrLf )
        Console.WriteLine( formatter, "Char", _
            "Decimal value", "bits(3)", "bits(2)", _
            "bits(1)", "bits(0)" )
        Console.WriteLine( formatter, "----", _
            "-------------", "-------", "-------", _
            "-------", "-------" )
          
        ' Convert Char values and display the results.
        DecimalFromChar( Chr( 0 ) )
        DecimalFromChar( " "c )
        DecimalFromChar( "*"c )
        DecimalFromChar( "A"c )
        DecimalFromChar( "a"c )
        DecimalFromChar( "{"c )
        DecimalFromChar( ""c )
    End Sub 
End Module

' This example of the op_Implicit conversion from Char to Decimal generates the
' following output. It displays the Decimal value and its binary representation.
' 
'   Char  Decimal value   bits(3)  bits(2)  bits(1)  bits(0)
'   ----  -------------   -------  -------  -------  -------
'                     0  00000000 00000000 00000000 00000000
'                    32  00000000 00000000 00000000 00000020
'      *             42  00000000 00000000 00000000 0000002A
'      A             65  00000000 00000000 00000000 00000041
'      a             97  00000000 00000000 00000000 00000061
'      {            123  00000000 00000000 00000000 0000007B
'                  198  00000000 00000000 00000000 000000C6

[C#] 
// Example of the implicit conversion from char to decimal.
using System;

class DecimalFromCharDemo
{
    const string formatter = 
        "{0,6}{1,15}{2,10:X8}{3,9:X8}{4,9:X8}{5,9:X8}";

    // Convert the char argument and display the decimal value.
    public static void DecimalFromChar( char argument )
    {
        decimal decValue;
        int[ ]  bits;

        // Display the decimal and its binary representation.
        decValue = argument;
        bits = decimal.GetBits( decValue );

        Console.WriteLine( formatter, argument, decValue, 
            bits[ 3 ], bits[ 2 ], bits[ 1 ], bits[ 0 ] );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the implicit conversion from char to " +
            "decimal generates the \nfollowing output. It displays " +
            "the decimal value and its binary representation.\n" );
        Console.WriteLine( formatter, "char", 
            "decimal value", "bits[3]", "bits[2]", 
            "bits[1]", "bits[0]" );
        Console.WriteLine( formatter, "----", 
            "-------------", "-------", "-------", 
            "-------", "-------" );

        // Convert char values and display the results.
        DecimalFromChar( '\0' );
        DecimalFromChar( ' ' );
        DecimalFromChar( '*' );
        DecimalFromChar( 'A' );
        DecimalFromChar( 'a' );
        DecimalFromChar( '{' );
        DecimalFromChar( '' );
    }
}

/*
This example of the implicit conversion from char to decimal generates the
following output. It displays the decimal value and its binary representation.

  char  decimal value   bits[3]  bits[2]  bits[1]  bits[0]
  ----  -------------   -------  -------  -------  -------
                    0  00000000 00000000 00000000 00000000
                   32  00000000 00000000 00000000 00000020
     *             42  00000000 00000000 00000000 0000002A
     A             65  00000000 00000000 00000000 00000041
     a             97  00000000 00000000 00000000 00000061
     {            123  00000000 00000000 00000000 0000007B
                 198  00000000 00000000 00000000 000000C6
*/

[C++] 
// Example of the op_Implicit conversion from __wchar_t to Decimal.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = 
    L"{0,10}{1,15}{2,10:X8}{3,9:X8}{4,9:X8}{5,9:X8}";

// Convert the __wchar_t argument and display the Decimal value.
void DecimalFromChar( __wchar_t argument )
{
    Decimal decValue;
    Int32   bits[ ];

    // The compiler invokes a constructor in the Managed Extensions 
    // for C++ unless op_Implicit is explicitly called.
    decValue = Decimal::op_Implicit( argument );

    // Display the Decimal and its binary representation.
    bits = Decimal::GetBits( decValue );
    Console::WriteLine( formatter, __box( argument ), 
        __box( decValue ), __box( bits[ 3 ] ), 
        __box( bits[ 2 ] ), __box( bits[ 1 ] ), __box( bits[ 0 ] ) );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the op_Implicit conversion from " 
        S"__wchar_t to Decimal \ngenerates the following output. " 
        S"It displays the Decimal value and \nits binary "
        S"representation.\n" );
    Console::WriteLine( formatter, S"__wchar_t", 
        S"Decimal value", S"bits[3]", S"bits[2]", 
        S"bits[1]", S"bits[0]" );
    Console::WriteLine( formatter, S"---------", 
        S"-------------", S"-------", S"-------", 
        S"-------", S"-------" );

    // Convert __wchar_t values and display the results.
    DecimalFromChar( L'\0' );
    DecimalFromChar( L' ' );
    DecimalFromChar( L'*' );
    DecimalFromChar( L'A' );
    DecimalFromChar( L'a' );
    DecimalFromChar( L'{' );
    DecimalFromChar( L'' );
}

/*
This example of the op_Implicit conversion from __wchar_t to Decimal
generates the following output. It displays the Decimal value and
its binary representation.

 __wchar_t  Decimal value   bits[3]  bits[2]  bits[1]  bits[0]
 ---------  -------------   -------  -------  -------  -------
                        0  00000000 00000000 00000000 00000000
                       32  00000000 00000000 00000000 00000020
         *             42  00000000 00000000 00000000 0000002A
         A             65  00000000 00000000 00000000 00000041
         a             97  00000000 00000000 00000000 00000061
         {            123  00000000 00000000 00000000 0000007B
                     198  00000000 00000000 00000000 000000C6
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | Char

Show:
© 2014 Microsoft