简体   繁体   中英

C# tostring(radix)

I have the following line in javascript:

c = Number(string_1.charCodeAt(i) ^ string_2.charCodeAt(u)).toString(16);

I need to rewrite it in c#, this is what I got so far:

string c = (Convert.ToChar(string_1[i]) ^ Convert.ToChar(string_2[u])).ToString(16);

I'm not able to enter radix value in ToString method. Any suggestions how I can do this? Thanks

You can use Convert.ToString to write out a value in a different base (Note that only certain bases are supported; 16 is one of them, see docs for details) :

int i = 16;
var str = Convert.ToString(i, 16);

Just change it to .ToString("X"); which is the hexadecimal format specifier.

Or even easier, if you're looking for base 16 (aka hexadecimal):

int x = 12345 ;
string v = string.Format( "0x{0:X4}" , x ) ;

will give you

v = "0x3039"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM