Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Fast algorithm for padding nulls into a string

Fast algorithm for padding nulls into a string

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmstutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matthias 0
    wrote on last edited by
    #1

    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

    U S 2 Replies Last reply
    0
    • M Matthias 0

      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

      U Offline
      U Offline
      Uwe Keim
      wrote on last edited by
      #2

      Why not use the sprintf() CRT function with some formatting flags?

      M 1 Reply Last reply
      0
      • U Uwe Keim

        Why not use the sprintf() CRT function with some formatting flags?

        M Offline
        M Offline
        Matthias 0
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • M Matthias 0

          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

          P Offline
          P Offline
          Paolo Messina
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Matthias 0

            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

            S Offline
            S Offline
            Sam Hobbs
            wrote on last edited by
            #5

            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.

            B 1 Reply Last reply
            0
            • S Sam Hobbs

              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.

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups