Csharp/C Sharp by API/System/Convert
Содержание
Convert.ChangeType
using System;
public class MainClass {
public static void Main() {
Type targetType = typeof(int);
object source = "42";
object result = Convert.ChangeType(source, targetType);
Console.WriteLine(result); // 42
Console.WriteLine(result.GetType()); // System.Int32
}
}
Convert.FromBase64CharArray
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
foreach (byte b in Base64DecodeString("AAAA"))
Console.WriteLine(b);
}
public static byte[] Base64DecodeString(string inputStr)
{
byte[] encodedByteArray = Convert.FromBase64CharArray(inputStr.ToCharArray(), 0, inputStr.Length);
return (encodedByteArray);
}
}
Convert.FromBase64String
using System;
using System.IO;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] b = Encoding.Unicode.GetBytes("string");
// Return the Base64-encoded string.
string str = Convert.ToBase64String(b);
Console.WriteLine(str);
Convert.FromBase64String(str);
// Return the decoded Unicode string.
Console.WriteLine(Encoding.Unicode.GetString(b));
}
}
Convert.ToBase64CharArray
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
static void Main(string[] args){
Console.WriteLine(Base64EncodeBytes(new byte[5] {45,34,23,54,38}));
}
public static string Base64EncodeBytes(byte[] inputBytes)
{
// Each 3 byte sequence in inputBytes must be converted to a 4 byte sequence
long arrLength = (long)(4.0d * inputBytes.Length / 3.0d);
if ((arrLength % 4) != 0)
{
// increment the array lenght to the next multiple of 4 if it is not already divisible by 4
arrLength += 4 - (arrLength % 4);
}
char[] encodedCharArray = new char[arrLength];
Convert.ToBase64CharArray(inputBytes, 0, inputBytes.Length, encodedCharArray, 0);
return (new string(encodedCharArray));
}
}
Convert.ToBase64String
using System;
using System.IO;
using System.Text;
class MainClass
{
public static void Main()
{
byte[] b = Encoding.Unicode.GetBytes("string");
// Return the Base64-encoded string.
string str = Convert.ToBase64String(b);
Console.WriteLine(str);
Convert.FromBase64String(str);
// Return the decoded Unicode string.
Console.WriteLine(Encoding.Unicode.GetString(b));
}
}
Convert.ToInt32
using System;
using System.Data;
class Class1{
static void Main(string[] args){
string base2 = "111";
string base8 = "117";
string base10 = "1110";
string base16 = "11F1FF";
Console.WriteLine("Convert.ToInt32(base2, 2) = " +
Convert.ToInt32(base2, 2));
Console.WriteLine("Convert.ToInt32(base8, 8) = " +
Convert.ToInt32(base8, 8));
Console.WriteLine("Convert.ToInt32(base10, 10) = " +
Convert.ToInt32(base10, 10));
Console.WriteLine("Convert.ToInt32(base16, 16) = " +
Convert.ToInt32(base16, 16));
}
}
Convert.ToUInt32
using System;
public class MainClass {
public static void Main() {
int thirty = Convert.ToInt32("1E", 16); // parse in hexadecimal
uint five = Convert.ToUInt32("101", 2); // parse in binary
}
}