十六进制到长双c++
Hex to long double c++
任何人都知道如何在c++中将长双精度转换为十六进制并从十六进制反转为长双精度。我已经使用istringstream将HEXstring转换为long-long并将其强制转换为double,但不知道如何转换long-long,以及如何测试类似的东西。
如果你真的确定要这样做,你可以最大限度地使用联合来"欺骗类型系统"。显然,这是原始内存访问,并附带了大量注意事项。最重要的是,它只是"通常"工作,但不受语言规范的保证
下面是输出长双精度原始字节的代码。从十六进制字符串重新组装长双精度的代码应该不会太难提供给您自己。
#include <stdio.h>
typedef union {
long double ld;
char bytes[sizeof(long double)];
} converter;
int main() {
converter c;
c.ld = 3.49;
printf("0x");
for (int i = 0; i < sizeof(long double); ++i) {
printf("%x", c.bytes[i]);
}
printf("n");
}
很抱歉发布死讯。无论如何,这是我的解决方案。它应该在long double
是类型的任何平台上工作。
除了位字段外,对象由一个或多个字节的连续序列组成,每个字节由CHAR_bit位组成,并且可以使用memcpy复制到unsigned CHAR[n]类型的对象中,其中n是对象的大小。结果数组的内容称为对象表示。
请参阅:C对象严格别名
// C11
#include <stdio.h> // printf
#include <string.h> // memcpy, size_t, NULL
#include <assert.h> // assert macro
int main(void) {
const long double src = 3.1415926L;
unsigned char dest[sizeof src] = { 0 };
assert (memcpy(dest, &src, sizeof src) != NULL);
printf("%Lf in hex: 0x", src);
for (size_t i = 0ul; i < sizeof src; ++i)
printf("%X", dest[i]);
}
实例
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 指向存储在字符串 c++ 中的十六进制
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C ++如何使用UTF8十六进制代码打印UTF8符号?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何在C++中将十六进制字符串转换为文本数据
- 使用 sprintf 将十六进制0xAABBCC转换为字符串"AA:BB:CC"
- 绝对编码器十六进制输入
- 为什么C++总是显示十六进制内存地址,而不仅仅是整数?
- C++17 十六进制浮点文字单精度后缀冲突?
- 是否可以在C++中获取 CHAR 的有效十六进制地址?
- 打印十六进制字符
- 编译器始终打印十六进制/八进制数字