다음을 통해 공유


System.Convert를 사용하여 데이터 형식 변환

업데이트: 2007년 11월

System.Convert 클래스에서는 지원되는 변환을 위한 완벽한 메서드 집합을 제공합니다. 이 클래스에서는 언어와 무관하게 변환을 수행할 수 있으므로 공용 언어 런타임을 대상으로 하는 모든 언어에서 이 클래스를 사용할 수 있습니다. 다른 언어에서 데이터 형식을 변환하는 데는 다른 기술이 사용될 수 있습니다. 하지만 Convert 클래스를 사용하면 일반적인 모든 변환을 일반적인 형식으로 수행할 수 있습니다. 이 클래스는 확대 변환, 축소 변환 및 관련되지 않은 데이터 형식으로의 변환을 수행합니다. 예를 들어, String 형식을 수치 형식으로, DateTime 형식을 String 형식으로 그리고 String 형식을 Boolean 형식으로 변환할 수 있습니다. 사용 가능한 변환의 목록은 Convert 클래스에서 메서드 목록을 참조하십시오. Convert 클래스에서는 검사된 변환을 수행하며 이 변환이 지원되지 않는 경우 항상 예외를 발생시킵니다. 흔히 발생하는 예외는 OverflowException입니다. 지원되는 변환의 목록은 형식 변환표를 참조하십시오.

사용자는 변환하려는 값을 Convert 클래스에 있는 메서드 중 하나로 전달할 수 있으며 이때 반환된 값을 새 변수로 초기화할 수 있습니다. 예를 들어, 다음 코드는 Convert 클래스를 사용하여 String 값을 Boolean 값으로 변환합니다.

Dim myString As String = "true"
Try
   Dim myBool As Boolean = Convert.ToBoolean(myString)
   Console.WriteLine(myBool)
Catch e As FormatException
   Console.WriteLine("{0} is not a Boolean value.", myString)
End Try   
' myBool has a value of True.
string myString = "true";
try
{
   bool myBool = Convert.ToBoolean(myString);
   Console.WriteLine(myBool);
}
catch (FormatException)
{
   Console.WriteLine("{0} is not a Boolean value.", myString);
}
// myBool has a value of True.

또한 문자열을 수치 값으로 변환하려는 경우에 Convert 클래스가 유용합니다. 다음 코드 예제는 수치 문자가 포함된 문자열을 Int32 값으로 변환합니다.

Dim newString As String = "123456789"
Try
   Dim myInt As Integer = Convert.ToInt32(newString)
   Console.WriteLine(myInt)
Catch e As FormatException
   Console.WriteLine("{0} does not represent a number.", newString)   
Catch e As OverflowException
   Console.WriteLine("{0} is out of range of the integer type.", _
                     newString)
End Try
' myInt has a value of 123456789.
string newString = "123456789";
try
{
   int myInt = Convert.ToInt32(newString);
   Console.WriteLine(myInt);
}
catch (FormatException)
{
   Console.WriteLine("{0} does not represent a number.", 
                     newString);   
}
catch (OverflowException)
{
   Console.WriteLine("{0} is out of range of the integer type.", 
                     newString);
}
// myInt has a value of 123456789.

또한, 특정 언어를 사용해서 암시적으로 수행할 수 없는 축소 변환의 경우에도 Convert 클래스를 사용할 수 있습니다. 다음 코드 예제는 Convert.ToInt32 메서드를 사용하여 Int64 형식을 크기가 작은 Int32 형식으로 축소 변환합니다.

Dim myInt64 As Int64 = 123456789
Try
   Dim myInt As Integer = Convert.ToInt32(myInt64)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", _ 
                     myInt64)
End Try
' MyInt has a value of 123456789.
Int64 myInt64 = 123456789;
try
{
   int myInt = Convert.ToInt32(myInt64);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to a 32-bit integer.", 
                     myInt64);
}
// myInt has a value of 123456789.

일부 경우에 Convert 클래스를 사용하여 축소 변환을 수행하면 변환되는 항목 값이 변경될 수 있습니다. 다음 코드 예제는 Double 형식을 Int32 형식의 값으로 변환합니다. 이 경우, 정상적으로 변환을 수행하기 위해 42.72가 43으로 반올림됩니다.

Dim myDouble As Double = 42.72
Try
   Dim myInt As Integer = Convert.ToInt32(myDouble)
   Console.WriteLine(myInt)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble)
End Try   
' MyInt has a value of 43.
Double myDouble = 42.72;
try 
{
   int myInt = Convert.ToInt32(myDouble);
   Console.WriteLine(myInt);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert {0} to an integer.", myDouble);
}
// myInt has a value of 43.

참고 항목

개념

명시적 변환

기타 리소스

형식 변환