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

0x0000 0000 0000 0000

CPallini 7-Feb-14 8:49am
I see a blank after two bytes, in your example.

1 solution

You must split the value into four 16-bit values and print them separate:
unsigned long long val = 0x1234567890123456ULL;
printf("0x%04x %04x %04x %04x\n",
    (unsigned)(val >> 48ULL) & 0xFFFF, 
    (unsigned)(val >> 32ULL) & 0xFFFF, 
    (unsigned)(val >> 16) & 0xFFFF, 
    (unsigned)val & 0xFFFF);
Member 3975629 7-Feb-14 9:25am
Thank you very much. I am trying it now. Appreciate your help.

Member 3975629 10-Feb-14 3:12am
I would like to put it into a string using sscanf as below so that i can display it in an edit box. But it is crashing. could you please let me know how i can put the formatted value in a string and while retrieving it i should be able to get it as a normal hexa decimal number so that i can do calculations with that Hexa number.

Appreciate your help.

Jochen Arndt 10-Feb-14 3:21am
To print it to a string, use sprintf() passing a buffer of sufficient size.

To convert an entered string, remove all spaces and pass the result to strtoul() with base = 16. By using base 16, the string is always treated as hex number even when the '0x' prefix is missing.

When using MFC, you can use the CString class. It provides the Format() member function similar to sprintf() and the Replace() function to remove the spaces (use Remove(" ", "")).
Member 3975629 10-Feb-14 3:45am
Thanks. it is working. BTW I am using CEGUI library for edit box stuff/GUI . i am looking into how i can do with CEGUI as you suggested solution.
Thanks a lot. I will contact you if i encounter any further issues. Appreciate your Help.
Jochen Arndt 10-Feb-14 4:03am
You are welcome.
When writing the above comment I forgot that you are working with 64-bit numbers. strtoul() handles only 32-bit numbers. With the Microsoft compiler, you can use _strtoui64() instead. With other compilers check if there is a similar function for 64-bit values. If not, you can still use sscanf().
Member 3975629 17-Feb-14 2:26am
I am trying to convert an unsigned long long val = 0x1234567890123456; to a string "0x1234567890123456" but i am not aqble 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.
Member 3975629 17-Feb-14 2:27am
the code is here:

#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtoul */
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);
return 0;
Jochen Arndt 17-Feb-14 3:00am
Are you using Visual C++? To print 64-bit integers with Microsoft Visual C++ there is the 'I64' type prefix. So you must use format '%#I64x'.
Member 3975629 17-Feb-14 3:06am
Yes. i am using VC++ and it supposed to work on Linux as well.
Do I need to do something like this ? I did but it is giving some other symbols.
Please let me know how to use your suggestion in code (it should work on both windows and Linux)

_snprintf(precisionMod, sizeof(precisionMod), ".%'%#I64x'", precision);
Jochen Arndt 17-Feb-14 3:14am
Then you must differentiate between Visual C++ and Linux (in fact the Microsoft C standard libary and the GNU library). I would use:
#ifdef _MSC_VER
sprintf(output, "%#16I64x", val);
sprintf(output, "%#16llx", val);
_MSC_VER is always defined when using Visual C++.
Member 3975629 17-Feb-14 3:44am
Thank you very much. it seems working.
I have one question. How can i use the above in my format. I tried as follows in my code . but it seems not working. Please correct me if I am wrong. Appreciate your help.

_snprintf(precisionMod, sizeof(precisionMod), "%#16I64x", precision);
Jochen Arndt 17-Feb-14 4:06am
If you want to specify the precision at runtime, you must create a format string:
char format[16];
sprintf(format, "%%#%uI64X", precision);
sprintf(output, format, val);
Note the usage of the double percent sign to print a single one to the format string.
Member 3975629 17-Feb-14 5:05am
when i use it in my code as follows it is giving the out put as follows

with the below code
sprintf(formatStr, "%%#%0uI64X", precision);
sprintf(formatStr,"%%%s%s%s",precisionMod, typeMod);
is there any way that we can pad with zeros on left side of the hexa string
Jochen Arndt 17-Feb-14 5:10am
To pad left with zeroes, you must use:
sprintf(formatStr, "%%#0%uI64X", precision);
I will explain:
%% = percent character
# = show hex prefix 0x or 0X
0 = pad left with zeroes
%u = width value
I64 = value is 64-bit
X = print as hex with upper case characters
Member 3975629 17-Feb-14 5:55am
Thanks a lot. Awesome explanation. most of my doubts are cleared.
if you see in my code i am trying to use _snprintf as follows. i've tried to fit the fix given by you as follows. but it seems not working as expected. please let me know if did any mistake here.
if (precision > 0)
_snprintf(precisionMod, sizeof(precisionMod), ".%uI64", precision);
Jochen Arndt 17-Feb-14 6:20am
My bad: I used the width to specify the precision but forgot to add the two hex prefix characters.

There are two ways to print leading zeroes:
sprintf(format, "%%#0%uI64X", precision + 2);
sprintf(format, "%%#.%uI64X", precision);
The first uses the width with '0' prefix which must include the width of the 0x prefix and the second uses the precision which is prefixed with a the period.
However, when omitting the '#' prefix and printing it in front, the width is identical to the precision:
sprintf(format, "0x%%#0%uI64X", precision);

I would not create the format string in multiple steps like you do. Just print the complete format string at once. If you need different formats, create them using one sprintf() command.

Using _snprintf() is not necessary here when setting the format buffer size to the max. possible length.

You will also have to use another compiler check because the function is named snprintf() with Linux (no leading underscore).
Member 3975629 17-Feb-14 8:14am
on Linux it is giving the below error ? any ideas/suggestions appreciated ?

error: format '%llX' expects type 'long long unsigned int', but argument 3 has type 'int'

I did code like this.
int main(){
char result[255] = {0};
char formatStr[20] = {0};

#ifdef _MSC_VER
sprintf(formatStr, "%%0%uI64X", precision);
sprintf(formatStr, "%%0%llX", precision);

char* output = result;
if (prefix)
sprintf(output, "0x");
output += 2;

sprintf(output, formatStr, val);
Jochen Arndt 17-Feb-14 8:19am
The format creation is wrong (missing an 'u'). It must be:
sprintf(formatStr, "%%0%ullX", precision);
Member 3975629 17-Feb-14 9:10am
Thanks a lot. It is working. You saved my day dear friend. I appreciate your timely help.

Member 3975629 18-Feb-14 2:22am
I am trying the below code in C++. it is returning some other value but i am expecting
0xFFFFFFFFFFFFFFFF. Is there any mistake here ?

If i give 0x0fff ffff ffff fffb it is working as i expected as below 0xFFF FFFF FFFF FFFB" Please let me know any ideas ? thanks.

std::string str = "0xFFFFFFFFFFFFFFFF";

long long val2 = _strtoi64(str.c_str() , NULL, 16);
it is retuning 0x7ffffffffffffff but i am expecting 0xFFFFFFFFFFFFFFFF
Jochen Arndt 18-Feb-14 2:51am
_strtoi64() converts a string to a signed value. Your value is too big and causes an overflow. To indicate this, _strtoi64() returns _I64_MAX. When working with hex numbers, it may be better to used the unsigned version _strtoui64(). However, this depends on the type of your input data (signed or unsigned).
Member 3975629 18-Feb-14 6:35am
Thank you. It is working as expected after using _strtoui64() method
Member 3975629 18-Feb-14 9:31am
when i use miValue = _strtoui64(strValue.c_str() , NULL, 16); it is compiling and working fine on windows. but on gentoo linux it is giving the below error
error: '_strtoui64' was not declared in this scope

any ideas /suggestions appreciated.
Jochen Arndt 18-Feb-14 9:45am
The equivalent function is strtoull().

When I have such multi platform projects, I often create a header file for Visual C that is included by each source file and maps the functions. It then contains macros and inline functions like this:
#ifdef _MSC_VER
inline unsigned __int64 strtoull(const char *nptr, char **endptr, int base)
{ return _strtoui64(nptr, endptr, base); }
inline int strcasecmp(const char *s1, const char *s2) { return stricmp(s1, s2); }
Then use only the Linux functions in the source.
Member 3975629 20-Feb-14 4:40am
I used the above code .while compiling on visual studio it is giving the below error

error C3861: 'strtoull': identifier not found

I added #include <stdlib.h> also. Please let me know how to fix this error. Appreciate your help.
Jochen Arndt 20-Feb-14 4:52am
Then the macro is not defined for the code section. Put the definition on top of your source file and it should work (but it would be better to hacve it in header file).
Member 3975629 20-Feb-14 5:53am
Thanks. it is working fine after putting it in top.
Member 3975629 20-Feb-14 8:15am
do we need strcasecmp macro here ?? I am just using strtoull.
is it for just example or do we need strcasecmp for strtoull to work ?

Please let me know your suggestions/ideas on this ?

My understanding is I can remove strcasecmp related code in the above macro since i am using strtoull .

Jochen Arndt 20-Feb-14 8:16am
It was just another example for a function that has different names with Microsoft and Linux.
Member 3975629 20-Feb-14 8:21am
Thank You
Member 3975629 20-Feb-14 3:10am
I have a method in my code which takes a void* as parameter.

to pass a hexa decimal value it is using the following code.
I am not able to understand why it was written like that. I tried to print
the value (FinalValue) it is printing just a differnt address (hexa decimal) value every time.

Could you please explain me any suggestion/ideas on this below code ?
Will the below code work for big hexa decimal string like "0xFFFFFFFFFFFFFFFF" without any code change ?? I am not able to understand why it is taking half of the length of original string.

Basically i could not understand it's output/intention.
Appreciate your help

void main()
//std::string seedStr = "0xFFFFFFFFFFFFFFFF";
std::string strValue = "0x12";
int strValuelen = 0;

unsigned char* FinalValue= new unsigned char[(strValue.length()/2)+1];
memset(FinalValue,0, (strValue.length()/2)+1);

//convert the strValue to hex
char* strValueHex = (char*)strValue.c_str();
int temp;
for(unsigned int i = 0; i < strValue.length()/2; ++i )
sscanf( strValueHex + 2 * i, "%2x", &temp );
FinalValue[i] = temp;

std::cout << ((void *)FinalValue);


Member 3975629 20-Feb-14 3:47am
if i type cast the Finalvalue to char* it is printing some symbols ╠↕
i am not sure what are those symbols ? ╠↕
are they equivalnet to "0x12" in hexa ??
Jochen Arndt 20-Feb-14 4:50am
This won't work. You should not ignore compiler errors and fix them by using casts. There is a reason that std::string.c_str() returns a 'const char*': You are not allowed to modifiy the buffer.
When using C++, you should not use C-style casts like (char*). Use static_cast<char>*() instead. Then your comiler will tell you if the casting is allowed or not.

Overall, these questions are not realted to the original one. If you have new problems you should ask a new question rather than using the comment section of an old question as chat system. New questions will be read by much more people incrasing the chance to get faster and better answers.
Member 3975629 10-Mar-14 4:52am
Hi Jochen,

Could you please look into the below and let me know if you have any ideas on that ? I would like to dynamically format the hex string into groups of 4 numbers.I mean based on the length of the value variable in the question i need to format instead of hard coding with shift operator many times.

Appreciate your help.

Jochen Arndt 10-Mar-14 8:31am
I have posted a solution there.

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