PART II
C h a p t e r 2 1 :
E x p l o r i n g t h e S y s t e m N a m e s p a c e
655
Method
Meaning
public static long
DoubleToInt64Bits(double
v
)
Conver ts
v
into a
long
integer and returns the
result.
public static byte[ ] GetBytes(bool
v
)
Conver ts
v
into a 1-byte array and returns the
result.
public static byte[ ] GetBytes(char
v
)
Conver ts
v
into a 2-byte array and returns the
result.
public static byte[ ] GetBytes(double
v
)
Conver ts
v
into an 8-byte array and returns the
result.
public static byte[ ] GetBytes(float
v
)
Conver ts
v
into a 4-byte array and returns the
result.
public static byte[ ] GetBytes(int
v
)
Conver ts
v
into a 4-byte array and returns the
result.
public static byte[ ] GetBytes(long
v
)
Conver ts
v
into an 8-byte array and returns the
result.
public static byte[ ] GetBytes(shor t
v
)
Conver ts
v
into a 2-byte array and returns the
result.
public static byte[ ] GetBytes(uint
v
)
Conver ts
v
into a 4-byte array and returns the
result.
public static byte[ ] GetBytes(ulong
v
)
Conver ts
v
into an 8-byte array and returns the
result.
public static byte[ ] GetBytes(ushor t
v
)
Conver ts
v
into a 2-byte array and returns the
result.
public static double
Int64BitsToDouble(long
v
)
Conver ts
v
into a
double
value and returns the
result.
public static bool ToBoolean(byte[ ]
a
, int
idx
)
Converts the byte at
a
[
idx
] into its
bool
equivalent
and returns the result. A non-zero value is
converted to true; zero is converted to false.
public static char ToChar(byte[ ]
a
, int
star t
)
Conver ts two bytes star ting at
a
[
star t
] into its
char
equivalent and returns the result.
public static double ToDouble(byte[ ]
a
,
int
star t
)
Conver ts eight bytes star ting at
a
[
star t
] into its
double
equivalent and returns the result.
public static shor t ToInt16(byte[ ]
a
, int
star t
)
Conver ts two bytes star ting at
a
[
star t
] into its
short
equivalent and returns the result.
public static int ToInt32(byte[ ]
a
, int
star t
)
Conver ts four bytes star ting at
a
[
star t
] into its
int
equivalent and returns the result.
public static long ToInt64(byte[ ]
a
, int
star t
)
Conver ts eight bytes star ting at
a
[
star t
] into its
long
equivalent and returns the result.
public static float ToSingle(byte[ ]
a
, int
star t
)
Conver ts four bytes star ting at
a
[
star t
] into its
float
equivalent and returns the result.
T
ABLE
21-13
Methods Defi ned by
BitConverter
www.freepdf-books.com
656
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
Random_Numbers_with_Random'>Generating Random Numbers with Random
To generate a sequence of pseudorandom numbers, you will use the
Random
class.
Sequences of random numbers are useful in a variety of situations, including simulations
and modeling. The starting point of the sequence is determined by a
seed
value, which can
be automatically provided by
Random
or explicitly specified.
Random
defines these two constructors:
public Random( )
public Random(int
seed
)
The first version creates a
Random
object that uses the system time to compute the seed
value. The second uses the value of
seed
as the seed value.
Random
defines the methods shown in Table 21-14.
Here is a program that demonstrates
Random
by creating a pair of computerized dice:
// An automated pair of dice.
using System;
class RandDice {
static void Main() {
Random ran = new Random();
Console.Write(ran.Next(1, 7) + " ");
Console.WriteLine(ran.Next(1, 7));
}
}
public static string ToString(byte[ ]
a
)
Conver ts the bytes in
a
into a string. The string
contains the hexadecimal values associated
with the bytes, separated by hyphens.
public static string ToString(byte[ ]
a
, int
star t
)
Conver ts the bytes in
a,
beginning at
a
[
star t
], into a string. The string contains the
hexadecimal values associated with the bytes,
separated by hyphens.
public static string ToString(byte[ ]
a
, int
star t
,
int
count
)
Converts the bytes in
a,
beginning at
a
[
start
] and
running for
count
bytes, into a string. The string
contains the hexadecimal values associated with
the bytes, separated by hyphens.
public static ushor t ToUInt16(byte[ ]
a
, int
star t
)
Conver ts two bytes star ting at
a
[
star t
] into its
ushort
equivalent and returns the result.
public static uint ToUInt32(byte[ ]
a
, int
star t
)
Conver ts four bytes star ting at
a
[
star t
] into its
uint
equivalent and returns the result.
public static ulong ToUInt64(byte[ ]
a
, int
star t
)
Conver ts eight bytes star ting at
a
[
star t
] into its
ulong
equivalent and returns the result.
T
ABLE
21-13
Methods Defi ned by
BitConverter
(continued)
Method
Meaning
www.freepdf-books.com
Do'stlaringiz bilan baham: |