Reputation: 21
I'm doing a switch case where the case values need to be in binary digits like 01,010,0100,0,00,000,
and so on.
switch(Code)
{
case 0:
{
printf("A");
break;
}
case 00:
{
printf("B");
break;
}
case 000:
{
printf("C");
break;
}
I know that if I input 000, it it will be stored as 0. My question is how do you input 000 so that I can get printf("C")
Upvotes: 1
Views: 3434
Reputation: 228
Given you're using this for Morse (I'm guessing 0 = Dot, 1 = dash) in your variable. "Code" Must be a String.
switch(Code)
{
case "01": //".-"
printf("A");
break;
case "100": //"-.."
printf("B");
break;
case "0001": //"...-"
printf("V");
break;
}
Upvotes: 0
Reputation: 9589
Your problem is you have a series of bits that you are trying to decode (signal on the wire). But you don't know when your packet of information starts. What you are missing in the encoding is the start of a letter. (You also don't have start of word, I'd recommend making up an unused number to represent a space between words).
In your program would recommend prefixing each series of bits with a 1 start bit (note this is not actually transmitted but implied because of the time duration of nothing being transmitted). Then your values are 10, 100, and 1000. You can easily compare these in binary representation. Note the smallest valid series of bits is then the letter 'e' which would be 10 in binary. A single 1 bit is not a valid value. Just from glancing at the Morse code entry here it would appear that the alphabet forms a numbering system that could be stored in a lookup table for a fast implementation.
Upvotes: 2
Reputation: 579
I can't imagine why you would want to distinguish between these numbers, as in any representation they are the same number, but I guess you will have to examine them as strings. You can't put strings into a switch statement in C, so you will just have to have a sequence of if/else statements:
void selectString(char *input)
{
if(strcmp(input, "0") == 0)
{
printf("A");
}
else if(strcmp(input, "00") == 0)
{
printf("B");
}
else if(strcmp(input, "000") == 0)
{
printf("C");
}
}
Having said this, what you are asking appears very much like an XY problem, that is, you are asking how to do weird thing Y because you think you need to do it to solve X. What are you trying to do?
Upvotes: 3