Fast algorithm for padding nulls into a string
-
Hello, I need to create a string from an int. The string should be always 6 characters long and prefixed by "0"'s if the int doesn't have 6 digits. For example: 5 should become "000005" 8940 should become "008940" I hope this explains what I'm going to do. Problem is, the algorithm will be executed very heavily, so I need the fastest solution available. Thanks for your help Matthias
-
Hello, I need to create a string from an int. The string should be always 6 characters long and prefixed by "0"'s if the int doesn't have 6 digits. For example: 5 should become "000005" 8940 should become "008940" I hope this explains what I'm going to do. Problem is, the algorithm will be executed very heavily, so I need the fastest solution available. Thanks for your help Matthias
-
And can't figure out how it should be working. There are flags for append 0's but I can't see flags for prefixing 0's. Can you provide some code? Matthias
-
And can't figure out how it should be working. There are flags for append 0's but I can't see flags for prefixing 0's. Can you provide some code? Matthias
Excuse the intrusion... sprintf("0x%08X", value); This will print a hex number with exactly 8 char, prefixed with zeroes. For a decimal number "%08d" should be fine. Cheers, Paolo.
-
Hello, I need to create a string from an int. The string should be always 6 characters long and prefixed by "0"'s if the int doesn't have 6 digits. For example: 5 should become "000005" 8940 should become "008940" I hope this explains what I'm going to do. Problem is, the algorithm will be executed very heavily, so I need the fastest solution available. Thanks for your help Matthias
If you want to try to get better performance than what you get using sprintf, it might be faster to: (1) use _itoa to convert the number into a temporary result, then (2) use strncpy to copy a string of six zeros to the result, using 6 minus the length of the string in the temporary result for the count (3) concatenate the temporary result after that I am not sure if there will be situations where strncpy will not add a terminating null; if there will, then you will need to modify this algorithm accordingly. For example, you could just initialize the result to six zeros always, then copy (strcpy) the temporary result to the appropriate position in the result, depending on the size of the temporary result.
-
If you want to try to get better performance than what you get using sprintf, it might be faster to: (1) use _itoa to convert the number into a temporary result, then (2) use strncpy to copy a string of six zeros to the result, using 6 minus the length of the string in the temporary result for the count (3) concatenate the temporary result after that I am not sure if there will be situations where strncpy will not add a terminating null; if there will, then you will need to modify this algorithm accordingly. For example, you could just initialize the result to six zeros always, then copy (strcpy) the temporary result to the appropriate position in the result, depending on the size of the temporary result.
I did some brief testing using a debug build. I discovered that the Sprintf() took about 4100 ticks (difference between GetTickCount() calls before and after for loop) to do 1 million iterations. Using a technique similar to what was described in other replies took about 1500 ticks. Here is code snippet of faster technique. // preset to required zero characters memset(szBuffer, 48, 6); // convert your number to ascii string _itoa(iValue, szValue, 10); // get length of string iLength = lstrlen(szValue); // copy value into zero buffered string // starting at correct offset to leave leading zeroes lstrcpy(&szBuffer[6 - iLength], szValue); If you inlined the source for each of the functions, it would be even faster.