-2
output.WriteLine($"{Convert.ToString(num1, 16).PadLeft(width)}");

The above code outputs the integer converted to hexadecimal. The 'width' variable is a variable that takes a value from the keyboard to determine how to align the numbers. When printing hexadecimal numbers, I want to print the alphabet in uppercase.

I've done a google search with several words but couldn't find a way to tell this. I need to use the Convert.ToString function because I need to use the 'width' variable to enter the number of spaces to align the characters with. If anyone knows how to fix this, please help.

When I asked the question, I was mistaken and asked the question incorrectly in octal. It's corrected.

enter image description here

I want to print '94e' in the picture above as '94E'.

  • 1
    Can you not just use `.ToUpper()` or am I missing part of the requirement? – Ryan Thomas Jan 27 '23 at 10:18
  • 5
    Octal only has digits 01234567 – digits only exist as digits, there are now lower case digits nor upper case digits. Could you show the current output and your expected output? – knittl Jan 27 '23 at 10:19
  • 1
    Maybe you mean hexadecimal numbers (base 16) ? - these also use the letters a..f. – wohlstad Jan 27 '23 at 10:28
  • @knittl I was mistaken, sorry. I want to output the alphabet in upper case when converting the integer to hexadecimal and outputting it. A photo was also uploaded. – Lee Seonghyeon Jan 27 '23 at 10:35
  • @wohlstad you're right. I was wrong, and I have corrected the question. – Lee Seonghyeon Jan 27 '23 at 10:38
  • You can use `Convert.ToString(num1, 16).ToUpper()` as suggested in the comment above. – wohlstad Jan 27 '23 at 10:40
  • For having a number of spaces, see https://stackoverflow.com/questions/411752/best-way-to-repeat-a-character-in-c-sharp – Dan Dumitru Jan 27 '23 at 10:44

1 Answers1

0

You can use Hexadecimal format specifier (X) for convertion to upper case

int input = 123;
string result = Convert.ToString(input, 16).ToUpper(); // "7B"
string result2 = input.ToString("x").ToUpper(); // "7b" ToUpper -> "7B"
string result3 = input.ToString("X"); // capital X here  "7B"

last one is my prefered approach

fubo
  • 44,811
  • 17
  • 103
  • 137