本文整理汇总了C#中System.BitConverter.ToInt32方法的典型用法代码示例。如果您正苦于以下问题:C# BitConverter.ToInt32方法的具体用法?C# BitConverter.ToInt32怎么用?C# BitConverter.ToInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.BitConverter
的用法示例。
在下文中一共展示了BitConverter.ToInt32方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Create an Integer from a 4-byte array.
Byte[] bytes1 = { 0xEC, 0x00, 0x00, 0x00 };
Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
BitConverter.ToInt32(bytes1, 0));
// Create an Integer from the upper four bytes of a byte array.
Byte[] bytes2 = BitConverter.GetBytes(Int64.MaxValue / 2);
Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes2),
BitConverter.ToInt32(bytes2, 4));
// Round-trip an integer value.
int original = (int) Math.Pow(16, 3);
Byte[] bytes3 = BitConverter.GetBytes(original);
int restored = BitConverter.ToInt32(bytes3, 0);
Console.WriteLine("0x{0:X4} ({0:N0}) --> {1} --> 0x{2:X4} ({2:N0})", original,
FormatBytes(bytes3), restored);
}
private static string FormatBytes(Byte[] bytes)
{
string value = "";
foreach (var byt in bytes)
value += String.Format("{0:X2} ", byt);
return value;
}
}
输出:
EC 00 00 00 --> 0x00EC (236) FF FF FF FF FF FF FF 3F --> 0x3FFFFFFF (1,073,741,823) 0x1000 (4,096) --> 00 10 00 00 --> 0x1000 (4,096)
示例2: DecimalToByteArray
//引入命名空间
using System;
using System.IO;
class MainClass
{
public static byte[] DecimalToByteArray (decimal src){
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static decimal ByteArrayToDecimal (byte[] src){
using (MemoryStream stream = new MemoryStream(src)){
using (BinaryReader reader = new BinaryReader(stream)){
return reader.ReadDecimal();
}
}
}
public static void Main()
{
byte[] b = null;
b = BitConverter.GetBytes(true);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToBoolean(b, 0));
b = BitConverter.GetBytes(3678);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToInt32(b, 0));
b = DecimalToByteArray(285998345545.563846696m);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(ByteArrayToDecimal(b));
}
}