karin
karin

Reputation: 96

String must be exactly one character long

I have what I think is an easy problem. For some reason the following code generates the exception, "String must be exactly one character long".

int n = 0;
foreach (char letter in charMsg)
{
    // Get the integral value of the character.
    int value = Convert.ToInt32(letter);
    // Convert the decimal value to a hexadecimal value in string form.
    string hexOutput = String.Format("{0:X}", value);
    //Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
    charMsg[n] = Convert.ToChar(hexOutput);
    n++;
}

The exception occurs at the charMsg[n] = Convert.ToChar(hexOutput); line. Why does it happen? When I check the values of CharMsg it seems to contain all of them properly, yet still throws an error at me.

UPDATE: I've solved this problem, it was my mistake. Sorry for bothering you.

OK, this was a really stupid mistake on my part. Point is, with my problem I'm not even supposed to do this as hex values clearly won't help me in any way.

What I am trying to do it to encrypt a message in an image. I've already encrypted the length of said message in last digits on each color channel of first pixel. Now I'm trying to put the very message in there. I lookt here: http://en.wikipedia.org/wiki/ASCII and said to myself without thinking that usung hexes would be a good idea. Can't belive I thought that.

Upvotes: 0

Views: 20212

Answers (4)

Alexander
Alexander

Reputation: 4173

Since printable unicode characters can be anywhere in range from 0x0000 to 0xFFFF, your hexOutput variable can hold more than one character - this is why error is thrown. Convert.ToChar(string) would always check length a of string, and if it is not equal to 1 - it would throw. So it would not convert string 0x30 to hexadecimal number, and then to ascii representation, symbol 0.

Can you elaborate on what you are trying to archieve ?

Upvotes: 3

GURU
GURU

Reputation: 25

Convert.ToChar(string) if it is empty string lead this error. instead use cchar()

Upvotes: -1

Nicholas Carey
Nicholas Carey

Reputation: 74227

Convert.ToChar( string s ), per the documentation requires a single character string, otherwise it throws a FormatException as you've noted. It is a rough, though more restrictive, equivalent of

public char string2char( string s ) { return s[0] ; }

Your code does the following:

  • Iterates over all the characters in some enumrable collection of characters.
  • For each such character, it...
    • Converts the char to an int. Hint: a char is an integral type: its an unsigned 16-bit integral value.
    • converts that value to a string containing a hex representation of the character in question. For most characters, that string will be at least two character in length: for instance, converting the space character (' ', 0x20) this way will give you the string "20".
    • You then try to convert that back to a char and replace the current item being iterated over. This is where your exception is thrown. One thing you should note here is that altering a collection being enumerated is likely to cause the enumerator to throw an exception.

What exactly are you trying to accomplish here. For instance, given a charMsg that consist of 3 characters, 'a', 'b' and 'c', what should happen. A clear problem statement helps us to help you.

Upvotes: 3

Reacher Gilt
Reacher Gilt

Reputation: 1813

Your hexOutput is a string, and I'm assuming charMsg is a character array. Suppose the first element in charMsg is 'p', or hex value 70. The documentation for Convert.ToChar(string) says it'll use just the first character of the string ('7'), but it's wrong. It'll throw this error. You can test this with a static example, like charMsg[n] = Convert.ToChar("70");. You'll get the same error.

Are you trying to replace characters with hex values? If so, you might try using a StringBuilder object instead of your array assignments.

Upvotes: 1

Related Questions