7 Replies - 4139 Views - Last Post: 21 December 2009 - 01:12 AM Rate Topic: -----

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Problem from converting char array to string

Posted 19 December 2009 - 01:48 PM

when I try to convert char[] to string using
			for (int i = 0; i < c.Length; i++)
				c[i] = (char)1;

			string r = new string (c);


the string becomes empty

why?
Is This A Good Question/Topic? 0
  • +

Replies To: Problem from converting char array to string

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Problem from converting char array to string

Posted 19 December 2009 - 01:53 PM

Because char are bytes, but different.

You're saying in the code to convert the int 1 to a char which maps to the "Start of Header" according the ASCII table.
Was This Post Helpful? 1
  • +
  • -

#3 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: Problem from converting char array to string

Posted 19 December 2009 - 02:13 PM

so what should I do to store the number in the string ?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: Problem from converting char array to string

Posted 19 December 2009 - 02:19 PM

Look up an ASCII table and see that the value of the CHARACTER 1 is.
Was This Post Helpful? 1
  • +
  • -

#5 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: Problem from converting char array to string

Posted 19 December 2009 - 03:30 PM

so This solved the problem for a part
but this one :

c[i] = (char)Math.Max(int.Parse(new string(a[i],1)), int.Parse(new string (b[i],1)));

I need to get the value of the max method to be a character
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Problem from converting char array to string

Posted 20 December 2009 - 03:07 AM

char[] charArray = {'f', 't', 'e', 'n', 'a', 'd'};
IEnumerable<string> stringArray = charArray.Cast<string>();

So, as I can understand, your goal is to store an integer value of char into string? Or am I wrong?
Regarding you last question: what are a and b variables?

This post has been edited by FlashM: 20 December 2009 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#7 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: Problem from converting char array to string

Posted 20 December 2009 - 01:51 PM

thank you all
the problem is solved

but FlashM
What is that line do ?
just for knowledge
IEnumerable<string> stringArray = charArray.Cast<string>();
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Problem from converting char array to string

Posted 21 December 2009 - 01:12 AM

Basically it means: Cast/Convert an array of chars into an enumerable array of strings.
IEnumerable is a collection class which is the base class for almost all the collection classes. In other words, almost all collection classes inherit functionalities from IEnumerable class.

One of the thing that IEnumerable class provides is that you can use foreach loop to iterate over all the elements in an array or list or any other collection that inherits from IEnumerable.


Anyway, I just tested the above code and it doesn't even work. The exception is thrown in runtime saying the it is not able to cast from char to string.

But the above explanation of IEnumerable class is still correct :-)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1