更新:2007 年 11 月
將 16 位元帶正負號整數的值,轉換為它在指定基底中的相等 String 表示。
命名空間:
System
組件:
mscorlib (在 mscorlib.dll 中)
Public Shared Function ToString ( _
value As Short, _
toBase As Integer _
) As String
Dim value As Short
Dim toBase As Integer
Dim returnValue As String
returnValue = Convert.ToString(value, _
toBase)
public static string ToString(
short value,
int toBase
)
public:
static String^ ToString(
short value,
int toBase
)
public static String ToString(
short value,
int toBase
)
public static function ToString(
value : short,
toBase : int
) : String
如果 value 為負,且 toBase 不是十進位值,則傳回的字串會使用兩個補數表示。
下列程式碼範例會使用 ToString 方法所支援的基數,以這個方法將數個 16 位元的整數轉換為 String。
' Example of the Convert.ToString( Short, Integer ) method.
Imports System
Imports Microsoft.VisualBasic
Module ConvertRadixShortDemo
Sub RunToStringDemo( )
Dim values as Short( ) = { _
Short.MinValue, _
-100, _
999, _
Short.MaxValue }
Dim radices as Integer( ) = { 2, 8, 10, 16 }
' Iterate through the values array.
Dim value as Short
For Each value in values
' Iterate through the radices.
Dim radix as Integer
For Each radix in radices
' Convert a value with a radix.
Dim valueString As String = _
Convert.ToString( value, radix )
Console.WriteLine( "{0,8} {1,3} {2}", _
value, radix, valueString )
Next radix
Next value
End Sub
Sub Main( )
Console.WriteLine( _
"This example of Convert.ToString( Short, Integer ) " & _
"generates " & vbCrLf & "the following output. It " & _
"converts several Short values to " & vbCrLf & "strings " & _
"using the radixes supported by the method." )
Console.WriteLine( vbCrLf & _
" Value Radix String" & vbCrLf & _
" ----- ----- ------" )
RunToStringDemo( )
End Sub
End Module
' This example of Convert.ToString( Short, Integer ) generates
' the following output. It converts several Short values to
' strings using the radixes supported by the method.
'
' Value Radix String
' ----- ----- ------
' -32768 2 1000000000000000
' -32768 8 100000
' -32768 10 -32768
' -32768 16 8000
' -100 2 1111111110011100
' -100 8 177634
' -100 10 -100
' -100 16 ff9c
' 999 2 1111100111
' 999 8 1747
' 999 10 999
' 999 16 3e7
' 32767 2 111111111111111
' 32767 8 77777
' 32767 10 32767
' 32767 16 7fff
// Example of the Convert.ToString( short, int ) method.
using System;
class ConvertRadixShortDemo
{
static void RunToStringDemo( )
{
short[ ] values = {
short.MinValue,
-100,
999,
short.MaxValue };
int[ ] radices = { 2, 8, 10, 16 };
// Iterate through the values array.
foreach( short value in values )
{
// Iterate through the radices.
foreach( int radix in radices )
{
// Convert a value with a radix.
string valueString =
Convert.ToString( value, radix );
Console.WriteLine( "{0,8} {1,3} {2}",
value, radix, valueString );
}
}
}
static void Main( )
{
Console.WriteLine(
"This example of Convert.ToString( short, int ) " +
"generates \nthe following output. It converts several " +
"short values to \nstrings using the radixes supported " +
"by the method." );
Console.WriteLine(
"\n Value Radix String" +
"\n ----- ----- ------" );
RunToStringDemo( );
}
}
/*
This example of Convert.ToString( short, int ) generates
the following output. It converts several short values to
strings using the radixes supported by the method.
Value Radix String
----- ----- ------
-32768 2 1000000000000000
-32768 8 100000
-32768 10 -32768
-32768 16 8000
-100 2 1111111110011100
-100 8 177634
-100 10 -100
-100 16 ff9c
999 2 1111100111
999 8 1747
999 10 999
999 16 3e7
32767 2 111111111111111
32767 8 77777
32767 10 32767
32767 16 7fff
*/
// Example of the Convert::ToString( short, int ) method.
using namespace System;
using namespace System::Collections;
void RunToStringDemo()
{
array<short>^values = {Int16::MinValue, -100,999,Int16::MaxValue};
int radices[4] = {2,8,10,16};
// Implement foreach( short value in values ).
IEnumerator^ myEnum = values->GetEnumerator();
while ( myEnum->MoveNext() )
{
short value = Convert::ToInt16( myEnum->Current );
// Iterate through the radices.
for ( int i = 0; i < 4; i++ )
{
// Convert a value with a radix.
int radix = radices[ i ];
String^ valueString = Convert::ToString( value, radix );
Console::WriteLine( "{0,8} {1,3} {2}", value, radix, valueString );
}
}
}
int main()
{
Console::WriteLine( "This example of Convert::ToString( short, int ) "
"generates \nthe following output. It converts several "
"short values to \nstrings using the radixes supported "
"by the method." );
Console::WriteLine( "\n Value Radix String"
"\n ----- ----- ------" );
RunToStringDemo();
}
/*
This example of Convert::ToString( short, int ) generates
the following output. It converts several short values to
strings using the radixes supported by the method.
Value Radix String
----- ----- ------
-32768 2 1000000000000000
-32768 8 100000
-32768 10 -32768
-32768 16 8000
-100 2 1111111110011100
-100 8 177634
-100 10 -100
-100 16 ff9c
999 2 1111100111
999 8 1747
999 10 999
999 16 3e7
32767 2 111111111111111
32767 8 77777
32767 10 32767
32767 16 7fff
*/
// Example of the Convert.ToString( short, int ) method.
import System.* ;
class ConvertRadixShortDemo
{
static void RunToStringDemo()
{
short values[] = { -32768, -100, 999, 32767 };
int radices[] = { 2, 8, 10, 16 };
// Iterate through the values array.
for (int iCtr = 0; iCtr < values.length; iCtr++) {
short value = values[iCtr];
// Iterate through the radices.
for (int iCtr1 = 0 ; iCtr1 < radices.length ; iCtr1++) {
int radix = radices[iCtr1];
// Convert a value with a radix.
String valueString = Convert.ToString(value, radix);
Console.WriteLine("{0,8} {1,3} {2}",
System.Convert.ToString(value),
System.Convert.ToString(radix), valueString);
}
}
} //RunToStringDemo
public static void main(String[] args)
{
Console.WriteLine(("This example of Convert.ToString( short, int ) "
+ "generates \nthe following output. It converts several "
+ "short values to \nstrings using the radixes supported "
+ "by the method."));
Console.WriteLine(("\n Value Radix String"
+ "\n ----- ----- ------"));
RunToStringDemo();
} //main
} //ConvertRadixShortDemo
/*
This example of Convert.ToString( short, int ) generates
the following output. It converts several short values to
strings using the radixes supported by the method.
Value Radix String
----- ----- ------
-32768 2 1000000000000000
-32768 8 100000
-32768 10 -32768
-32768 16 8000
-100 2 1111111110011100
-100 8 177634
-100 10 -100
-100 16 ff9c
999 2 1111100111
999 8 1747
999 10 999
999 16 3e7
32767 2 111111111111111
32767 8 77777
32767 10 32767
32767 16 7fff
*/
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
支援版本:3.5、2.0、1.0
XNA Framework
支援版本:2.0、1.0
參考