System::BitConverter 类

BitConverter class

包含执行字节序列与值类型相互转换的方法。这是一个没有实例服务的静态类型。任何情况下都不应创建其实例。

class BitConverter

方法

方法描述
static _IsLittleEndian()指示当前架构的字节序。
static DoubleToInt64Bits(double)返回一个 64 位整数值,其二进制表示等于指定双精度浮点值的二进制表示。
static GetBytes(bool)将指定的布尔值转换为字节数组。
static GetBytes(char_t)将指定的 char_t 值转换为字节数组。
static GetBytes(int16_t)将指定的 16 位整数值转换为字节数组。
static GetBytes(int)将指定的 32 位整数值转换为字节数组。
static GetBytes(int64_t)将指定的 64 位整数值转换为字节数组。
static GetBytes(uint16_t)将指定的无符号 16 位整数值转换为字节数组。
static GetBytes(uint32_t)将指定的无符号 32 位整数值转换为字节数组。
static GetBytes(uint64_t)将指定的无符号 64 位整数值转换为字节数组。
static GetBytes(float)将指定的单精度浮点值转换为字节数组。
static GetBytes(double)将指定的双精度浮点值转换为字节数组。
static Int64BitsToDouble(int64_t)返回一个双精度浮点值,其值等同于 value。
static ToBoolean(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的一个字节转换为布尔值。
static ToBoolean(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的一个字节转换为布尔值。
static ToChar(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为 char_t 值。
static ToChar(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为 char_t 值。
static ToDouble(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为双精度浮点值。
static ToDouble(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为双精度浮点值。
static ToInt16(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为 16 位整数值。
static ToInt16(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为 16 位整数值。
static ToInt32(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为 32 位整数值。
static ToInt32(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为 32 位整数值。
static ToInt64(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为 64 位整数值。
static ToInt64(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为 64 位整数值。
static ToSingle(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为单精度浮点值。
static ToSingle(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为单精度浮点值。
static ToString(const ArrayPtr<uint8_t>&, bool, const String&)将指定字节数组的所有值转换为其十六进制字符串表示形式。十六进制表示中使用的字母大小写以及在相邻字节对之间插入的分隔符通过相应参数指定。
static ToString(const ArrayPtr<uint8_t>&, int)从指定索引开始,将指定字节数组的值转换为其十六进制字符串表示形式。
static ToString(const ArrayPtr<uint8_t>&, int, int)将指定字节数组的一段值转换为其十六进制字符串表示形式。
static ToUInt16(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为无符号 16 位整数值。
static ToUInt16(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的两个字节转换为无符号 16 位整数值。
static ToUInt32(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为无符号 32 位整数值。
static ToUInt32(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的四个字节转换为无符号 32 位整数值。
static ToUInt64(const System::ArrayPtr<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为无符号 64 位整数值。
static ToUInt64(const System::Details::ArrayView<uint8_t>&, int)将指定数组中从指定索引开始的八个字节转换为无符号 64 位整数值。

字段

字段描述
static IsLittleEndian指示当前架构的字节序。如果架构为小端序则为 true,否则为 false。

备注

#include <system/bit_converter.h>
#include <system/smart_ptr.h>

using namespace System;

template <typename T>
void Print(T arg)
{
  std::cout << arg << ' ';

  for (const auto byte: BitConverter::GetBytes(arg))
  {
    std::cout << std::hex << static_cast<int>(byte);
  }

  std::cout << std::endl;
}

int main()
{
  // 创建要打印的值。
  int anInt = 1234567890;
  double aDouble = 0.123456789;

  // 打印值及其字节。
  Print(anInt);
  Print(aDouble);

  return 0;
}
/*
This code example produces the following output:
1234567890 d229649
0.123457 5f633937dd9abf3f
*/

另见