728x90
반응형
/*
 RGB를 받아 16진수로 바꿔 색상코드를 출력하려고 한다.
DecToHex 함수로 만들어 10진수를 16진수로 만드는 함수를 만들어보자.
아스키코드값을 이용해 10이상의 수 15이하의 수를 알파벳 대문자를 받게 만들자.

시행 착오
while(value != 0)
        {
            stack.Push(value % 16);
            value /= 16;
        }
로 값을 구했는데, 0이 들어가면 출력이 안되는 문제가 생겼다.
강제적으로 두번을 돌리는 방식으로 수정을 했다.
 */
using System;
using System.Collections.Generic;
public class Kata
{
  public static string Rgb(int r, int g, int b)
  {
      string result = "";
      result += DecToHex(r);
      result += DecToHex(g);
      result += DecToHex(b);

      return result;
  }
  public static string DecToHex(int value, int min = 0, int max = 255)
  {
      string result = "";
      value = Math.Clamp(value, min, max);
      Stack<int> stack = new Stack<int>();
      for(int i =0; i < 2; i++)
      {
          stack.Push(value % 16);
          value /= 16;
      }

      for(int i = stack.Count;  i > 0; i--)
      {
          int temp = stack.Pop();
          if(temp >= 10)
          {
              temp += 55;
          }
          else
          {
              temp += 48;
          }
          result += (char)temp;
      }

      return result;
  }
}
728x90
반응형

'프로그래밍 공부 > TIL' 카테고리의 다른 글

2025-07-02 TIL  (0) 2025.07.02
2025-06-30 TIL  (0) 2025.06.30
20250526 - AI  (0) 2025.05.26
20250521 - 코테 문제풀이  (0) 2025.05.21
20250520 - 움직이는 플랫폼  (0) 2025.05.20