C# hyung, string.Format essence conversion question

Asked 2 years ago, Updated 2 years ago, 36 views

Convert the string.Format to a format that spaces every four spaces, and convert the result value to hexadecimal and octal I'm trying to do it, but the format of hex and oct cannot be converted. I think it's because I wrote Convert to String, so I'd appreciate it if you could revise it.

tbNum.Text = string.Format("{0:###,###,##0}",result); 
isClear = true;

tbDec.Text = string.Format("{0:###,###,##0}", result);

tbHex.Text = string.Format("{0:#### #### ###0}",Convert.ToString(result, 16)); 
tbOct.Text = string.Format("{0:### ### ##0}", Convert.ToString(result,8)); 

c#

2022-09-21 10:43

1 Answers

I've never touched C#, but they kept asking me the same question, so I looked it up a little bit

The result value is a number and it looks like you're trying to print it out for a particular format.

The reason why the format is not applied to hex and oct is that the number changes to hex and octal strings after Tostring

Maybe the target for applying the format should be a number, so it can't be applied

public class Hello1 {
    public static void Main() {
        int result = 34243323;    
        System.Console.WriteLine(string.Format("{0:### ### ##0}", System.Convert.ToString(result, 8)));

        int r = System.Int32.Parse(System.Convert.ToString(result,8));
        System.Console.WriteLine("{0:### ### ##0}", r);
    }
}

When I ran it this way, I found that the format is not applicable if only the Toastring has passed, but it is applicable if converted back to numbers.

In the case of octal, since the number that can come to each digit is in the int range, there is no problem if this string is changed back to decimal, so that is possible

In hexadecimal numbers, the alphabet can come to each place, so if you do that, there will be an error.

So, I think you can parse the string and put a space in between.

And this time, I think it would be better if you looked for the method yourself rather than waiting for an answer


2022-09-21 10:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.