- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是一个方法-
using System;
class Program
{
static void Main(string[] args)
{
//
// Create an array of four bytes.
// ... Then convert it into an integer and unsigned integer.
//
byte[] array = new byte[4];
array[0] = 1; // Lowest
array[1] = 64;
array[2] = 0;
array[3] = 0; // Sign bit
//
// Use BitConverter to convert the bytes to an int and a uint.
// ... The int and uint can have different values if the sign bit differs.
//
int result1 = BitConverter.ToInt32(array, 0); // Start at first index
uint result2 = BitConverter.ToUInt32(array, 0); // First index
Console.WriteLine(result1);
Console.WriteLine(result2);
Console.ReadLine();
}
}
输出
1638516385
我只想知道这是怎么回事?
最佳答案
BitConverter.ToInt32
的文档实际上有一些很好的例子。假设 BitConverter.IsLittleEndian
返回 true,array[0]
是最低有效字节,如您所示...虽然 array[3]
不仅仅是符号位,它是最高有效字节,包括符号位(如第 7 位),但其余位用于大小。
所以在你的情况下,最低有效字节是 1,下一个字节是 64 - 所以结果是:
( 1 * (1 << 0) ) + // Bottom 8 bits
(64 * (1 << 8) ) + // Next 8 bits, i.e. multiply by 256
( 0 * (1 << 16)) + // Next 8 bits, i.e. multiply by 65,536
( 0 * (1 << 24)) // Top 7 bits and sign bit, multiply by 16,777,216
即 16385。如果设置了符号位,您需要以不同的方式考虑这两种情况,但在这种情况下很简单。
关于c# - BitConverter.ToInt32 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7993840/
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
通常情况下,如果你想在字节数组中表示 5,它会像 {0x00,0x00,0x00,0x05} 一样,但 BitConverter 给我反向数组({0x05,0x00,0x00,0x00})为什么会这样
我正致力于在 .net 核心中编写自己的 DNS 服务器。我正处于对响应有效负载进行编码以发回的阶段,架构显示大多数数字都编码为 16 位数字。 C# 的整数是 32 位数字。没什么大不了的,我只是去
我最近一直在研究 BitConverter 的工作原理,并通过阅读其他 SO 问题,我读到,当起始索引可被转换为可以强制转换的类型的大小整除时,它需要一条“捷径”将索引处的字节指针转换为指向要转换为的
我正在使用 BitConverter.ToInt32 将 3 个 byte 值打包成一个 int,如下所示: byte R = 0; byte G = 0; byte B = 0; int i = B
我尝试将 IP 地址转换为长值: byte[] Ip = new byte[4] { 192, 168, 1, 0 }; UInt32 Ret1 = (((UInt32)Ip[0]) << 24) |
我需要获取 UInt16 和 UInt64 中的值作为 Byte[]。目前我正在使用 BitConverter.GetBytes,但这种方法每次都会给我一个新的数组实例。 我想使用一种允许我将这些值“
我需要 C# BitConverter.DoubleToInt64Bits(doubleValue) 的 Swift 实现。我发现网站上的 C# 实现只是 https://referencesourc
我的代码旨在提取 4 字节消息中的两个中间字节,然后附加两个中间字节以形成一个 16 位无符号整数。不幸的是,我在使用 BitConverter.ToUint16 时遇到问题 - 我的代码似乎无法正常
创建起来非常简单。我有一个简单的字节数组,证明它在运行时有数据: 那我就干 var bytedata = BitConverter.ToUInt32(byte_array,0); 它可以编译,但我在运
这是一个方法- using System; class Program { static void Main(string[] args) { // /
我最近遇到一种情况,我需要创建一个通用方法来从字节数组中读取数据类型。 我创建了以下类: public class DataStream { public int Offset { get;
这个问题在这里已经有了答案: How do you convert a byte array to a hexadecimal string, and vice versa? (52 个答案) 关闭
我正在尝试将一些使用 BitConverter 的现有 C# 代码移植到 Java。我发现了各种其他线程,但后来偶然发现了一个 github 类,它似乎可以解决这个问题。但是,ToUInt16 与我的
在我的程序中,我有一个 PacketFactory,它构建准备好通过 NetworkStream 发送的缓冲区。正如您在下面看到的,此方法采用 short 作为参数。此短消息传递给 BitConver
我正在尝试使用 BitConverter.GetBytes 获取不同类型的不同对象的字节数.为此,我想编写一个通用扩展方法,而不是为我想使用的每种类型(短、长、ulong 等)编写单独的扩展方法。 这
我想知道是否存在与 .Net 的 BitConverter.GetBytes() 方法等效的 Objective C。 例如,在C#中,我可以这样写: byte[] lengthPrefix = Bi
我正在尝试读取文件的 PE header 以获取一些信息。对于 .NET 和 C#,我使用 BitConverter 将读取文件后获得的字节数组转换为等效的整数。我希望对 C++ 做同样的事情,但不确
BitConverter.ToString 给出格式为 'XX-XX-XX-XX' 的十六进制数 是否有相反的方法,以便我可以从以这种格式给出的字符串中获取原始字节数组? 最佳答案 没有,但是很容易实
只是想知道是否有人可以解释为什么以下两行代码返回“不同”的结果?是什么导致了相反的值(value)观?这与字节顺序有关吗? int.MaxValue.ToString("X") //Result: 7
我是一名优秀的程序员,十分优秀!