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. From primitive type to string

From primitive type to string

Scheduled Pinned Locked Moved C / C++ / MFC
algorithms
5 Posts 2 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.
  • S Offline
    S Offline
    S van Leent
    wrote on last edited by
    #1

    I want to know the algorithm used to make a string from an integer, double, etc., to a string and the other way around. Does anyone know this algorithm, I need to use this, because I'm making a garbage collected (Boehm-Weiser) string wrapper for use with TCHAR. Thanks in advance, Sjoerd van Leent LPCTSTR Dutch = TEXT("Double Dutch :-)");

    J 1 Reply Last reply
    0
    • S S van Leent

      I want to know the algorithm used to make a string from an integer, double, etc., to a string and the other way around. Does anyone know this algorithm, I need to use this, because I'm making a garbage collected (Boehm-Weiser) string wrapper for use with TCHAR. Thanks in advance, Sjoerd van Leent LPCTSTR Dutch = TEXT("Double Dutch :-)");

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Maybe you can have a look at the source code for itoa and related files. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      S 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Maybe you can have a look at the source code for itoa and related files. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        S Offline
        S Offline
        S van Leent
        wrote on last edited by
        #3

        Logical, but I need to have the source files to do that, as far as I know, these files are all libraries, that's kinda problem. Sjoerd van Leent LPCTSTR Dutch = TEXT("Double Dutch :-)");

        J 1 Reply Last reply
        0
        • S S van Leent

          Logical, but I need to have the source files to do that, as far as I know, these files are all libraries, that's kinda problem. Sjoerd van Leent LPCTSTR Dutch = TEXT("Double Dutch :-)");

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Maybe it depends on the installation, but at leat in mine the source code is under Microsoft Visual Studio/VC98/CRT/SRC Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          S 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Maybe it depends on the installation, but at leat in mine the source code is under Microsoft Visual Studio/VC98/CRT/SRC Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            S Offline
            S Offline
            S van Leent
            wrote on last edited by
            #5

            You're right, I can find it. I looked it somewhat and made my own code doing it. I've just finished the String to Integer algorithm.int String::ToInteger(String string) { size_t index = 0; bool leadSpace = true; bool isValue = true; bool minus = false; int value = 0; try { // first scanning if the string has non-numbers while(leadSpace) { TCHAR tcRet = string.GetCharAt(index); if (((tcRet < TEXT('0')) || (tcRet > TEXT('9')) ) && (tcRet != TEXT('-')) && (tcRet != TEXT('+'))) { index++; } else { leadSpace = false; } } // scan whether the first character is equal to '-' (45) or '+' (43) TCHAR tcRet = string.GetCharAt(index); if (tcRet == TEXT('-')) { minus = true; index++; } else if (tcRet == TEXT('+')) { index++; } // OK, now a number begins, I hope, if no number is found after the minus // or plus sign, we immediatly return 0 while (isValue) { TCHAR tcRet = string.GetCharAt(index); if (tcRet >= TEXT('0') && tcRet <= TEXT('9')) { int hold = (int)tcRet - (int)TEXT('0'); value *= 10; value += hold; index++; } else { if (minus) return -value; else return value; } } } catch (Exception ex) { throw (ex); } return 0; }
            this is the code I'm using now to do it (if someone likes it, just use it) I'm now working on ToDouble, ToBoolean (easy!), ToUnsignedInteger, etc... Sjoerd van Leent LPCTSTR Dutch = TEXT("Double Dutch :-)");

            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