[C++] 다양한 형식 간 변환 (형변환)


char *에서 변환
wchar_t에서 변환 *
_bstr_t에서 변환
CComBSTR에서 변환
CString에서 변환
basic_string에서 변환
Convert System:: String

https://docs.microsoft.com/ko-kr/cpp/text/how-to-convert-between-various-string-types?view=msvc-160



int to string
#include <iostream>
#include <sstream>

int main()
{
	int int_value = 3;

	std::ostringstream int_to_string;

	int_to_string << int_value;

	std::string string_value = int_to_string.str();

	std::cout << string_value.c_str() << std::endl;

	return 0;
}



string to int, double
#include <iostream>
#include <sstream>

int main()
{
	int int_value = 0;
	std::stringstream int_string_stream("123");
	int_string_stream >> int_value;

	double double_value = 0.0;
	std::stringstream double_string_stream("12.3456");
	double_string_stream >> double_value;

	if (!int_string_stream.fail())
	{
		std::cout << int_value << std::endl;
	}
	else
	{
		std::cout << "can't convert" << std::endl;
	}

	if (!double_string_stream.fail())
	{
		std::cout << double_value << std::endl;
	}
	else
	{
		std::cout << "can't convert" << std::endl;
	}

	return 0;
}

댓글

이 블로그의 인기 게시물

[NSIS] 32비트와 64비트 모듈 등록하는 법. (regsvr32)

[Visual Studio] Windows 7 에서 Visual Studio 6.0 디버그 시 프로세스 좀비되는 증상 해결 방법