>  기사  >  백엔드 개발  >  C#에서 정수를 16진수로 또는 그 반대로 변환하는 방법은 무엇입니까?

C#에서 정수를 16진수로 또는 그 반대로 변환하는 방법은 무엇입니까?

王林
王林앞으로
2023-09-11 09:37:02915검색

在 C# 中如何将整数转换为十六进制,反之亦然?

정수를 16진수로 변환

string.ToString() 확장 메서드를 사용하여 정수를 16진수로 변환할 수 있습니다.

Integer Value: 500
Hexadecimal Value: 1F4

16진수를 정수로 변환

16진수 값은 int.Parse 또는 Convert.ToInt32

int.Parse를 사용하여 정수로 변환할 수 있습니다. − 숫자의 문자열 표현을 부호 있는 32비트로 변환합니다. 반환 값은 작업이 성공했는지 여부를 나타냅니다.

Hexadecimal Value: 1F4
Integer Value: 500

Convert.ToInt32 - 지정된 값을 32비트 부호 있는 정수로 변환합니다.

Hexadecimal Value: 1F4
Integer Value: 500

정수를 16진수로 변환

string hexValue = IntegerValue.ToString("X");

Example

Live Demo

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         int integerValue = 500;
         Console.WriteLine($"Integer Value: {integerValue}");
         string hexValue = integerValue.ToString("X");
         Console.WriteLine($"Hexadecimal Value: {hexValue}");
         Console.ReadLine();
      }
   }
}

Output

위 코드의 출력은

rrre입니다. 에에

16진수를 정수로 변환

int.Parse를 사용하는 예

Example

Live Demo

Integer Value: 500
Hexadecimal Value: 1F4

Output

위 코드의 출력은

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         string hexValue = "1F4";
         Console.WriteLine($"Hexadecimal Value: {hexValue}");
         int integerValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
         Console.WriteLine($"Integer Value: {integerValue}");
         Console.ReadLine();
      }
   }
}

Convert.ToInt32를 사용하는 예입니다.

온라인 데모

Hexadecimal Value: 1F4
Integer Value: 500

Output

위 코드의 출력은

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         string hexValue = "1F4";
         Console.WriteLine($"Hexadecimal Value: {hexValue}");
         int integerValue = Convert.ToInt32(hexValue, 16);
         Console.WriteLine($"Integer Value: {integerValue}");
         Console.ReadLine();
      }
   }
}
입니다.

위 내용은 C#에서 정수를 16진수로 또는 그 반대로 변환하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제
이전 기사:C# 문자열 메서드다음 기사:C# 문자열 메서드