c++ cli - What is %c equivalent for String.Format in .NET? -


i tried find way port "%c" formatting in printf style function .net language, failed. example, how can write:

char code = 'a'; sprintf(text,"oops! %c",code); 

in c++/cli tried did not give me 'a'!

edit:

first tried format value character 'a' or 'b' or 'c'... environment visualstudio 2008, cli/cpp, .net 3.5. wrote test code show did.

string^ text; char    charc = 'a'; text = string::format("(1) oops! {0}",charc); outputdebugstring(text); char    charv = 1; text = string::format("(2) oops! {0}",charv + 0x60); outputdebugstring(text); text = string::format("(3) oops! {0}",(char)(charv + 0x60)); outputdebugstring(text); int intv = 1; text = string::format("(4) oops! {0}",(char)(intv + 0x60)); outputdebugstring(text); 

result different expected.

(1) oops! 97 (2) oops! 97 (3) oops! 97 (4) oops! 97 

above code seems working differently code suggested others. , feel sorry should have stated did in more detail @ first time.

if have {0} format variable, have no choice select making character 'a' or number '97'. , failed 'a', wanted ask how it.

edit2:

as suggested comment, changed 'char' 'char' this;

string^ text; char    charc = 'a'; text = string::format("(1) oops! {0}",charc); outputdebugstring(text); char    charv = 1; text = string::format("(2) oops! {0}",charv + 0x60); outputdebugstring(text); text = string::format("(3) oops! {0}",(char)(charv + 0x60)); outputdebugstring(text); int intv = 1; text = string::format("(4) oops! {0}",(char)(intv + 0x60)); outputdebugstring(text); 

and got;

(1) oops! (2) oops! 97 (3) oops! (4) oops! 

so 'char' instead of 'char' seems working.

the char keyword keeps original meaning in c , c++ languages, 8-bit type in msvc++, .net framework not have equivalent character type string::format() treats alias system::sbyte. integral type see integer value , not character.

.net uses utf-16 encoded unicode, system::char 16-bit type. in msvc++ matches wchar_t keyword. can use directly, or use char instead of char.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -