2

I want to generate a 4 character hex number.

To generate a hex number you can use

string.format("{0:X}", number) 

and to generate a 4 char string you can use

string.format("{0:0000}", number)

Is there any way to combine them?

2

2 Answers 2

5

I'm assuming you mean: 4-digit hexadecimal number.

If so, then yes:

string.Format("{0:X4}", number)

should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

4

Have you tried:

string hex = string.Format("{0:X4}", number);

? Alternatively, if you don't need it to be part of a composite pattern, it's simpler to write:

string hex = number.ToString("X4");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.