CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Thumbs down Convert Numbers to/from Strings problem

    Hi all,

    We are doing a project which optimizes the source code of our system.
    This system is very old and written by VC6.

    Two of optimized contents are:
    - Replace all conversion functions, which convert Strings to Numbers, with strtod and strtol functions. The reasons are to use error checking mechanism of strtod and strtol functions (Check overflow).
    - The second content is to optimize conversion functions, which convert Numbers to Strings.
    I think using itoa function is a bad idea because itoa is not a C standard function. Using sscanf is not good, because it has no error checking mechanism. I am thinking about using sprintf() function, but someone told me that this function is not safetype.

    So could you please provide some suggestion on the second content?

    Should I use STL class?

    Regards,
    Tevez
    Last edited by tevez; March 19th, 2009 at 12:32 PM.

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Convert Numbers to/from Strings problem

    sprintf_s is a safe extension to sprintf
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Re: Convert Numbers to/from Strings problem

    My system is written by VC6, so I could not use secure version of API functions.

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Convert Numbers to/from Strings problem

    Download and use the latest SDK.
    I guess you should be able to do it in VC6 then.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: Convert Numbers to/from Strings problem

    Why do you need overflow protection for converting a number to a string? While it makes sense in the other direction (a number contained in a string can be larger than the type it is to be converted to) the reverse is not true. As long as the buffer is big enough a number can be printed to the buffer.

    There are some other considerations, such as if you are working with floating point then there are some values that may not be valid (NAN, etc.) but they will still print to the text buffer that way and there are other methods of detecting that situation first (_isnan(), etc.).

    Maybe you are worried about the text buffer overflowing? In that case, use the '_s' versions and check for buffer overflow. Or use a string object: CString or std:string.

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: Convert Numbers to/from Strings problem

    Quote Originally Posted by _Superman_ View Post
    Download and use the latest SDK.
    I guess you should be able to do it in VC6 then.
    Aren't these function part of the c-runtime library and not the O.S.? It looks like I may have made the same mistake in suggesting their use.

  7. #7
    Join Date
    Mar 2009
    Posts
    3

    Re: Convert Numbers to/from Strings problem

    Thanks all for your answers.

    But, there are some points I want to mention here:

    1. Our system is very OLD and still running on our customer's machines a long time.
    We should not use the latest version of SDK to replace only itoa/ltoa function by sprintf_s.
    We want to use a replaced function which is the most simple and most efficient.

    2. I have ever thought about using str::string, but this is only available on C++/VC++.
    Our system's source code mixed between C and C++.
    Therefore we also should not use str::string.
    In additional, I read some information related to STL std::string class caused crashes and memory corruption (For example: http://support.microsoft.com/kb/813810).

    It is better for us if someone could provide us a solution of using C standard function.

    How about using snprintf function? I want to use this function because we can prevent our system from overflow of text buffer by providing a buffer size. And this function is simple and easy to use, especially I only want to convert from a long/integer number to string.
    But I don't know how to convert a long number that presented in exponent format to string.

    Please give me your suggestions.

    Thanks

  8. #8
    Join Date
    May 2002
    Posts
    1,435

    Re: Convert Numbers to/from Strings problem

    One thing that you need to do is clearly define your specifications. You say that you only want to convert a "long/integer number to a string" - sounds easy enough, what's the problem? And then you say you want to "convert a long number that presented in exponent format to string." I must admit that I don't know what you are talking about.

    You also say that you must use the solution in C language modules, but you wonder if you should use STL? How would you do that? It sounds to me like you are saying we want to use straight C functions but we don't want to use them because they aren't "safe." C++ has enriched programming capabilities substantially, but if you are limited to using C then you probably have no other options but to use the C functions.

    sprintf() is not type-safe? Well, good luck trying to replace it when limiting yourself to C - though snprintf() sounds like a good alternative to me. However, you would need extra error checking and possibly buffer re-allocation to deal with the errors. What type of efficiency are you looking for? Speed? Size? If speed, I think that any additional error checking and re-allocation you do is going to add extra execution time to the code. Same thing with size.

    It sound to me as if you are concerned about selecting the wrong option and that may be a valid concern. If this was my project, I would probably replace all of the instances of conversion in the old code with my own custom functions that could be centrally modified. My functions would be designed to guarantee success or present an error handling or exception mechanism.

    And finally, are you having problems with conversion in the old code? Is it crashing or giving you wrong results because of overflows? Once again, you claim that you want to "optimize" but maybe what you really need to do is debug the old code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured