如何将无符号长整型转换为所需格式的字符串 [英] How Do I Convert Unsigned Long Long To A String In The Desired Format
问题描述
我正在尝试转换无符号长long val = 0x1234567890123456;到一个字符串0x1234567890123456,但我不能够转换它,即使我使用%llu作为格式说明符与代码部分提供的代码。
但我能够转换unsigned int val = 0x1234567A;到0x1234567A。
你能否告诉我要做到这一点我必须做些什么?感谢您的帮助。
我想转换一个无符号长long val = 0x1234567890123456;使用以下代码到字符串0x1234567890123456。
I am trying to convert an unsigned long long val = 0x1234567890123456; to a string "0x1234567890123456" but i am not able to convert it even if i use "%llu" as format specifier with the code provided in code-section .
But I am able to convert an unsigned int val = 0x1234567A; to "0x1234567A".
Could you please let me know what i have to do to achieve this ?? Appreciate your help.
I want to convert an unsigned long long val = 0x1234567890123456; to a string "0x1234567890123456" with the below code.
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtoul */
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<wchar.h>
#include<iostream>
using namespace std;
int main ()
{
//unsigned long long val = 0x1234567890123456;
unsigned int val = 0x1234567A;
char result[255] = {0};
char formatStr[20] = {0};
char typeMod[2] = {0};
unsigned char precision=16;
sprintf(typeMod, "X");
char precisionMod[4] = {0};
if (precision > 0)
_snprintf(precisionMod, sizeof(precisionMod), ".%u", precision);
sprintf(formatStr, "%%%s%s%s", precisionMod, "", typeMod);
char* output = result;
sprintf(output, "0x");
output += 2;
sprintf(output, formatStr, val);
puts(result);
getch();
return 0;
}
推荐答案
使用Microsoft和GNU的C标准库,64位数字有不同的类型前缀。在为多个平台构建时,您可以使用:
There are different type prefixes for 64-bit numbers with the C standard libraries from Microsoft and GNU. When building for multiple platforms, you may use:
unsigned long long val = 0x1234567890123456;
#ifdef _MSC_VER // Using Visual C/C++
sprintf(output, "%#16I64X", val);
#else
sprintf(output, "%#16llX", val);
#endif
有关使用多平台编辑的信息已发布作为对如何显示/打印十六进制小数的问题的评论使用C ++使用C ++在4个字节后使用空格分隔符的数字如何以特殊格式显示Hexa十进制数,如下例所示,后面有空格... [ ^ ]。
这篇关于如何将无符号长整型转换为所需格式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!