Godfried23
Godfried23

Reputation: 41

Convert number to reversed array of digits

So I'm trying to convert a long to a reversed long array. This is what I have:

public static long[] Digitize(long n)
{
  string numConvert = n.ToString();

  char[] charArray = numConvert.ToCharArray();
  long[] charToLong = new long[charArray.Length]; 

  for (int i = 0; i < charArray.Length; i++)
    charToLong[i] = Convert.ToInt64(charArray[i]);

  return Array.Reverse(charToLong);
}

But every time I compile, I get this error: "Cannot implicitly convert type'void' to 'long[]'" Just what am I not doing right here?

Upvotes: 0

Views: 1488

Answers (3)

CodingYoshi
CodingYoshi

Reputation: 27049

Just call Reverse on the string, then turn each character back to a long and return it.

public static long[] Digitize(long n)
{
    var reversed = n.ToString().Reverse().Select(x => long.Parse(x.ToString())).ToArray();
    return reversed;
}

Upvotes: 2

user6710080
user6710080

Reputation: 97

You need to return the array, rather than the function. Array.Reverse() doesn't return anything (void), so after calling Array.Reverse(charToLong;), you need to return your array:

return charToLong;

Upvotes: 2

ispiro
ispiro

Reputation: 27753

Array.Reverse(charToLong);
return charToLong;

Array.Reverse returns void, not the reversed array. Click on the link and see the Syntax: public static void .

Upvotes: 2

Related Questions