Compiler independent wtoi function

This is a discussion on Compiler independent wtoi function within the C++ Programming forums, part of the General Programming Boards category; I'm wondering whether if there's a c++ standard counterpart of the MSVC++ _wtoi function which would be compiler independent. _wtoi ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Compiler independent wtoi function

    I'm wondering whether if there's a c++ standard counterpart of the MSVC++ _wtoi function which would be compiler independent.

    _wtoi works fine for me at the moment but I'm going to be compiling my program on a linux machine with possibly gcc later on and I don't want to hit a snag on this score.

    Or should I pursue this workaround that still isn't working
    Code:
    	unsigned integer i;
    	wistringstream wss;
    	wstring ws;
    
    	ws = some_wide_character_that_I_get_from_elsewhere;
    	wss.str(ws);
    	wss >> i;
    If _wtoi works regardless of the compiler/system, I'll go with it. But if not, advice would be appreciated.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what about atoi()?

    I would definately avoid using MSVC's 'standard' functions, because most of them... well... aren't. http://www.cppreference.com is a good place for finding standard functions and the like.

    afaik, a wchar_t is really just an int in a nice suit...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    what about atoi()?
    Wouldn't that be susceptible to undefined behavior if you through a two byte character at it? I'm not worried about whether it works on my XP. I'm worried about whether it'll break apart when it goes to gcc or linux.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well why are you using wchar_t just to hold numbers anyway? I'd worry about finding a way out of using wchar_t first.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I'm getting input from a unicode text file. I don't know if there's a way out of that.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I would figure out what is wrong with the stringstream version if you want to maintain portability. What problems are you having? Other than the use of "integer" instead of int, that code works for me in a simple test app.
    Last edited by Daved; 05-12-2006 at 03:29 PM.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    You could also have a look at boost::lexical_cast

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, here's a way to turn a single wchar_t into a character string with the same byte sequence:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
    	union switchitup
    	{
    		wchar_t bigletter;
    		char smalletters[sizeof(wchar_t)/sizeof(char)];
    	} abcd;
    
    	abcd.bigletter=0x0035;	
    	std::cout<<atoi(abcd.smalletters)<<std::endl;
    
    	return 0;
    }
    that wouldn't work for the entire UNICODE set though... you'd either need to build/find a conversion function or play with the bytes some more...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Why doesn't this work?
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
      std::wistringstream wstrm(std::wstring(L"1234"));
      unsigned int num;
      wstrm>>num;
      std::cout<<num;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks for the replies.

    What problems are you having? Other than the use of "integer" instead of int, that code works for me in a simple test app.
    In my original version, I wasn't actually putting a wchar_t variable into the wstring. I was iterating through a wstring with a wstring::iterator and when it came across a number, it would directly dump it into a temporary wstring. That somehow didn't work so I just dumped the iterator into a wchar_t and then dumped that into a wstring which again went into a wistringstream and now it's working.
    I know I should understand why this is working and the other isn't but I'll put that off to another day.

    well, here's a way to turn a single wchar_t into a character string with the same byte sequence:
    Code:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
    	union switchitup
    	{
    		wchar_t bigletter;
    		char smalletters[sizeof(wchar_t)/sizeof(char)];
    	} abcd;
    
    	abcd.bigletter=0x0035;	
    	std::cout<<atoi(abcd.smalletters)<<std::endl;
    
    	return 0;
    }
    Thanks. Since I only need the numerical characters, I think this might be the better solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler warning and a confusing function declaration.
    By PaulBlay in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 10:18 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21