I want to convert an integer to 3 character ascii string. For example if integer is 123, the my ascii string will also be "123". If integer is 1, then my ascii will be "001". If integer is 45, then my ascii string will be "045". So far I’ve tried Convert.ToString but could not get the result. How?
I want to convert an integer to 3 character ascii string. For example if
Share
Answer for the new question:
You’re looking for
String.PadLeft. Use it likemyInteger.ToString().PadLeft(3, '0'). Or, simply use the"0"custom format specifier. LikemyInteger.ToString("000").Answer for the original question, returning strings like
"0x31 0x32 0x33":Explanation:
ToString()converts your integer123into its string representation"123".PadLeft(3,'0')pads the returned string out to three characters using a0as the padding characterchar, so.Selectselects into this array0xthen the value of the characterchartointwill allow you to get the ASCII value (you may be able to skip this cast, I am not sure)String.Join(" ", ...)puts it all back together again with spaces in between