首页 > 解决方案 > 字节转换为 INT64,在引擎盖下

问题描述

再会。对于当前项目,我需要知道数据类型如何表示为字节。例如,如果我使用:

long three = 500;var bytes = BitConverter.GetBytes(three);

我得到值 244,1,0,0,0,0,0,0。我知道它是一个 64 位的值,并且 8 位进入一点,因此有 8 个字节。但是244和1是怎么组成500的呢?我试过用谷歌搜索它,但我得到的只是使用 BitConverter。我需要知道位转换器是如何工作的。如果有人可以向我指出一篇文章或解释这些东西是如何工作的,将不胜感激。

标签: c#bytedata-conversion

解决方案


这很简单。

BitConverter.GetBytes((long)1); // {1,0,0,0,0,0,0,0};
BitConverter.GetBytes((long)10); // {10,0,0,0,0,0,0,0};
BitConverter.GetBytes((long)100); // {100,0,0,0,0,0,0,0};
BitConverter.GetBytes((long)255); // {255,0,0,0,0,0,0,0};
BitConverter.GetBytes((long)256); // {0,1,0,0,0,0,0,0}; this 1 is 256
BitConverter.GetBytes((long)500); // {244,1,0,0,0,0,0,0}; this is yours 500 = 244 + 1 * 256

如果您需要源代码,您应该查看 Microsoft GitHub,因为实现是开源的 :) https://github.com/dotnet


推荐阅读