How to display a unsigned long long value with a space after 4 digits - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to display a unsigned long long value with a space after 4 digits

mandalikasudhakarmandalikasudhakar IndiaPosts: 2Member

Hi All, I am trying to display a hexa decimal value with length ranging from (40 digits to 60 digits) as a result of SHA1HMAC algorithm. I have to display that hexa number in the format of 4 digits and after that a space (eg: 0x1234 5678 90AB CDEF ....)

I am using the below code but i am getting a compile error on Linux it is giving the below error with the same above code though i have used unsignd long long as the data type for value .Please let me know if you have any idea/suggestion on this.

**error: right shift count >= width of type **

And also please let me know how to avoid hard coding of here with shifts. I mean here i used 11 groups of 4 digits by using >> (shift) operator. but is there any way that we can dynamically get the format string with different length of value ? and also what could be the max value of a int64 value ? i mean how many max digits can it have ?

Please let me know how to handle this scenario and fix this. It would be great if I dynamically format because the length of the hexa decimal number may vary(from 40 digits to 60 digits)

Appreciate your help.

Thanks, Sudhakar

code: `

ifdef _MSC_VER

inline unsigned __int64 strtoull(const char *nptr, char **endptr, int base) { return _strtoui64(nptr, endptr, base); }


std::string result = "123456789ABCDEF123456789ABCDEF1234567890ABCDEF" unsigned long long value = strtoull(result.c_str(),NULL,16 ) ;

std::string resultString; char formatString[58] = {0};

sprintf(formatString,"0x%04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X", (unsigned)(value >> 160ULL) & 0xFFFF, (unsigned)(value >> 144ULL) & 0xFFFF, (unsigned)(value >> 128ULL) & 0xFFFF, (unsigned)(value >> 112ULL) & 0xFFFF, (unsigned)(value >> 96ULL) & 0xFFFF, (unsigned)(value >> 80ULL) & 0xFFFF, (unsigned)(value >> 64ULL) & 0xFFFF, (unsigned)(value >> 48ULL) & 0xFFFF, (unsigned)(value >> 32ULL) & 0xFFFF, (unsigned)(value >> 16) & 0xFFFF, (unsigned)value & 0xFFFF);

    resultString = formatString;


Sign In or Register to comment.