Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
#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;
}
Posted
Updated 16-Feb-14 20:41pm
v2

1 solution

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


[EDIT]
The information on using multi platform compilations has been posted as comment to the question How to display/print hexa decimal number with space delimiter after 4 bytes using C++ using C++ How to display a Hexa decimal number in a special format as in the example below with spaces after...[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900