Jump to content
  • Advertisement
Sign in to follow this  
slyterence

narrowing wstrings

This topic is 4782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to write a wide string into a std::ostringstream as follows
for(int i = 0; i < temp.length(); ++i)
{
	oss << oss.narrow(temp, '%');
}


and I get the following error:
c:\...\file.cpp : warning C4244: 'argument' : conversion from 'std::allocator<_Ty>::value_type' to 'char', possible loss of data
        with
        [
            _Ty=wchar_t
        ]


Is there a better (standard) way of doing this? Particularly, I'd prefer to convert the wstring into a string, and then just append that to the ostringstream, because then I can reuse the narrowing conversion wherever I want from a conversion class. I tried boost::lexical_cast, but that didn't work, so I'm a bit stumped. Thanks

Share this post


Link to post
Share on other sites
Advertisement
here's something I made that we use at work:

//# Sherief "JD" N. Farouk
//# Created: Saturday, August 13, 2005, 3:33:37 PM
//# Last Modified: Monday, August 15, 2005, 9:50:54 PM
//# ext-h-gen-dep-std-boost-nolib
#ifndef __EXT_LEXICAL_CAST_H__
#define __EXT_LEXICAL_CAST_H__

#pragma once

#include <string>
#include <locale>

#include <boost/lexical_cast.hpp>

namespace boost
{
template<>
std::wstring lexical_cast<std::wstring, std::string>(std::string arg)
{
std::wstring result;
std::locale loc;
for(unsigned int i= 0; i < arg.size(); ++i)
{
result += std::use_facet<std::ctype<wchar_t> >(loc).widen(arg);
}
return result;
}

template<>
std::string lexical_cast<std::string, std::wstring>(std::wstring arg)
{
std::string result;
std::locale loc;
for(unsigned int i= 0; i < arg.size(); ++i)
{
result += std::use_facet<std::ctype<wchar_t> >(loc).narrow(arg);
}
return result;
}
}

#endif



Hope you find it useful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!