解码十六进制格式
Decoding hexadecimal format
我有这个十六进制字符串:
"0000803F00000000000000B4B410D1A90000803FB41051B500000034B41051350000803F000000000000000000C05B400000000000C06B400000000000D07440"
我知道它包含什么:
(1, 0, -1.192093e-007),
(-9.284362e-014, 1, -7.788287e-007),
(1.192093e-007, 7.788287e-007, 1),
(111, 222, 333).
是的,它是一个变换矩阵!
解码前 72 个字符(每个数字 8 个字符)很简单,您只需要除以 8 并使用 IEEE 浮点格式即。 0x0000803F = 1.0f
所以我们仍然有 "000000000000000000C05B400000000000C06B400000000000D07440"
包含第四个向量,但我从未见过这种数字编码。
有没有关于这个的?
看起来这些是 8 字节的 IEEE 浮点数,从字节 40 开始。所以布局是:
- 字节 0-11:第一个向量,3 个单精度数
- 字节 12-23:第二个向量,3 个单精度数
- 字节 25-35:第三个向量,3 个单精度数
- 字节 36-39:未使用? (填充?)
- 字节40-63:第四个向量,3个双精度数
下面的代码显示了在 C# 中对此进行解析的示例。代码的输出是:
(1, 0, -1.192093E-07)
(-9.284362E-14, 1, -7.788287E-07)
(1.192093E-07, 7.788287E-07, 1)
(111, 222, 333)
示例代码:
using System;
class Program
{
static void Main(string[] args)
{
string text = "0000803F00000000000000B4B410D1A90000803FB41051B500000034B41051350000803F000000000000000000C05B400000000000C06B400000000000D07440";
byte[] bytes = ParseHex(text);
for (int i = 0; i < 3; i++)
{
float x = BitConverter.ToSingle(bytes, i * 12);
float y = BitConverter.ToSingle(bytes, i * 12 + 4);
float z = BitConverter.ToSingle(bytes, i * 12 + 8);
Console.WriteLine($"({x}, {y}, {z})");
}
// Final vector
{
double x = BitConverter.ToDouble(bytes, 40);
double y = BitConverter.ToDouble(bytes, 48);
double z = BitConverter.ToDouble(bytes, 56);
Console.WriteLine($"({x}, {y}, {z})");
}
}
// From
public static byte[] ParseHex(string hex)
{
int offset = hex.StartsWith("0x") ? 2 : 0;
if ((hex.Length % 2) != 0)
{
throw new ArgumentException("Invalid length: " + hex.Length);
}
byte[] ret = new byte[(hex.Length-offset)/2];
for (int i=0; i < ret.Length; i++)
{
ret[i] = (byte) ((ParseNybble(hex[offset]) << 4)
| ParseNybble(hex[offset+1]));
offset += 2;
}
return ret;
}
static int ParseNybble(char c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
throw new ArgumentException("Invalid hex digit: " + c);
}
}
我有这个十六进制字符串:
"0000803F00000000000000B4B410D1A90000803FB41051B500000034B41051350000803F000000000000000000C05B400000000000C06B400000000000D07440"
我知道它包含什么:
(1, 0, -1.192093e-007),
(-9.284362e-014, 1, -7.788287e-007),
(1.192093e-007, 7.788287e-007, 1),
(111, 222, 333).
是的,它是一个变换矩阵!
解码前 72 个字符(每个数字 8 个字符)很简单,您只需要除以 8 并使用 IEEE 浮点格式即。 0x0000803F = 1.0f
所以我们仍然有 "000000000000000000C05B400000000000C06B400000000000D07440"
包含第四个向量,但我从未见过这种数字编码。
有没有关于这个的?
看起来这些是 8 字节的 IEEE 浮点数,从字节 40 开始。所以布局是:
- 字节 0-11:第一个向量,3 个单精度数
- 字节 12-23:第二个向量,3 个单精度数
- 字节 25-35:第三个向量,3 个单精度数
- 字节 36-39:未使用? (填充?)
- 字节40-63:第四个向量,3个双精度数
下面的代码显示了在 C# 中对此进行解析的示例。代码的输出是:
(1, 0, -1.192093E-07)
(-9.284362E-14, 1, -7.788287E-07)
(1.192093E-07, 7.788287E-07, 1)
(111, 222, 333)
示例代码:
using System;
class Program
{
static void Main(string[] args)
{
string text = "0000803F00000000000000B4B410D1A90000803FB41051B500000034B41051350000803F000000000000000000C05B400000000000C06B400000000000D07440";
byte[] bytes = ParseHex(text);
for (int i = 0; i < 3; i++)
{
float x = BitConverter.ToSingle(bytes, i * 12);
float y = BitConverter.ToSingle(bytes, i * 12 + 4);
float z = BitConverter.ToSingle(bytes, i * 12 + 8);
Console.WriteLine($"({x}, {y}, {z})");
}
// Final vector
{
double x = BitConverter.ToDouble(bytes, 40);
double y = BitConverter.ToDouble(bytes, 48);
double z = BitConverter.ToDouble(bytes, 56);
Console.WriteLine($"({x}, {y}, {z})");
}
}
// From
public static byte[] ParseHex(string hex)
{
int offset = hex.StartsWith("0x") ? 2 : 0;
if ((hex.Length % 2) != 0)
{
throw new ArgumentException("Invalid length: " + hex.Length);
}
byte[] ret = new byte[(hex.Length-offset)/2];
for (int i=0; i < ret.Length; i++)
{
ret[i] = (byte) ((ParseNybble(hex[offset]) << 4)
| ParseNybble(hex[offset+1]));
offset += 2;
}
return ret;
}
static int ParseNybble(char c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
throw new ArgumentException("Invalid hex digit: " + c);
}
}