[Mono-list] Incorporate hexidecimal in C# string

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Mon Oct 13 15:03:30 UTC 2014


> From: mono-list-bounces at lists.ximian.com [mailto:mono-list-
> bounces at lists.ximian.com] On Behalf Of Alf C Stockton
> 
> Please tell me how I would go about incorporating hex 0x02 and 0x03, Start
> Of Text and End of Text, into a string in C#.
> In C i would sprintf it but have as yet not found an equivalent in C#.
> In fact from the reading I have done I am no longer sure that C# can
> handle hex.

I'm not completely sure I get what you're asking, but I'm betting this is the answer:
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Examples:
int x = 127;
string xString = x.ToString("X2");  // result "7F"
string xString = x.ToString("X4");  // result "007F"

int x = 127;
int y = 255;
Console.WriteLine("hi {0:X2} there {1:X4}", x, y);  // result "hi 7F there 00FF"

Also:
byte[] buf = new byte[] { 8, 32, 64, 128, 192, 255 };
string bufStr = BitConverter.ToString(buf);  // Result "08-20-40-80-C0-FF"



More information about the Mono-list mailing list