How can I convert char to char * in C #?

how can i convert char to char * in c #?

I initialize the String object as follows:

String test=new String('c'); 

and I get this error:

Argument '1': cannot convert from 'char' to 'char*'

+4
source share
14 answers

This is a bit of a strange way to initialize a string if you know in advance what you want to store in it.

You can simply use:

 String test="c"; 

If you need the specific need to convert a char variable to a string, you can use the built-in ToString() function:

 String test = myCharVariable.ToString(); 
+15
source
 unsafe { char c = 'c'; char *ch = &c; } 

In your example, there is a String and a compilation error due to using one of the overloads of the String constructor, so I assume that you just need an array of characters, otherwise String and maybe not char* .

In this case:

 char c = 'c'; string s = c.ToString(); // or... string s1 = "" +c; 

Also available:

 unsafe { char c = 'c'; char* ch = &c; string s1 = new string(ch); string s2 = new string(c, 0); } 
+12
source
 string myString1 = new string(new char[] {'a'}); string myString2 = 'a'.ToString(); string myString3 = "a"; string myString4 = new string('a', 1); unsafe { char a = 'a'; string myString5 = new string(&a); } 
+6
source

There is no public constructor overload for a String that takes a single char parameter as a parameter. Closest match

 public String(char c, int count) 

which creates a new String that repeats char c count times. So you could say

 string s = new string('c', 1); 

There are other options. There is an open String constructor that takes char[] as a parameter:

 public String(char[] value) 

This will create a String that is initialized with Unicode characters in value . So you could say

 char c = 'c'; string s = new String(new char[] { c }); 

Another option is to say

 char c = 'c' string s = c.ToString(); 

But the simplest approach that most expects to see is

 string s = "c"; 

As for converting a char to char * , you cannot do this safely. If you want to use the overload of the public constructor for a String that takes a char * parameter as a parameter, you can do this:

 unsafe { char c = 'c'; char *p = &c; string s = new string(p); } 
+3
source

Cannot damage another answer:

 string test = string.Empty + 'c'; 
+2
source

The String class has many constructors, if all you need to do is create a string containing one character, you can use the following:

 String test = new String(new char[] { 'c' }); 
+1
source

If you hardcode it, is there a reason you just aren't using it:

 String test = "c"; 
+1
source

What about:

 var test = 'c'.ToString() 
+1
source

When using char in the String constructor, you must also specify the count parameter to indicate how many times this character should be added to the string:

 String test=new String('c', 1); 

See also here .

+1
source

use

 String test("Something"); 
0
source

String test = new String (new char [] {'c'});

0
source

The easiest way to do this conversion from your example is to simply change the type of quotation marks you use from single quotes

 String test = new String('c'); 

to double quotes and remove the constructor call:

 String test = "c"; 
0
source
 char c = 'R'; char *pc = &c; 
0
source

Using single quotes (as in your question: 'c' ) means you are creating a char . Using double quotes, for example. "c" means you are creating a string . These are not interchangeable types in C #.

A char* , as you know, is how strings are represented to some extent in C ++, and C # supports some of the C ++ conventions. This means that a char* can easily (for a programmer, at least) be converted to string in C #. Unfortunately, char is not char * at its core, so this cannot be done.

0
source

All Articles