1

I am trying to convert C# code to C

Original C# code is

Calling Hex2Binary method from below code

private string DEtoBinary(string HexDE)
        {
            string deBinary = "";
            for (int I = 0; I <= 15; I++)
            {
                deBinary = deBinary + Hex2Binary(HexDE.Substring(I, 1));

            }

            return deBinary;

        }

//Hex2Binary Method

private string Hex2Binary(string DE)
        {

            string myBinary = "";
            switch (DE)
            {
                case "0":
                    myBinary = "0000";
                    break;

                case "1":
                    myBinary = "0001";
                    break;
                    .
                    .
                    .
                 }
      }

But when I write in C, I take argument as shown below

    char *Hex2Binary(char DE[])
    {
    
        char *myBinary = "";
        switch (DE)
        {
            case "0":
             myBinary = "0000";
             break;
    
            case "1":
             myBinary = "0001";
             break;
    
            case "2":
             myBinary = "0010";
             break;
                     .         
                     .   

         }
    }

I am getting error as Switch quantity not an integer.

Community
  • 1
  • 1
user2357643
  • 69
  • 1
  • 1
  • 5
  • How many cases do you have? You could just use `if-else` instead. There may be other problems in your C code as well. I don't think C allows a `switch` on a string. – Tim Biegeleisen Oct 29 '15 at 03:05
  • I have 15 cases.My main problem is how to accept string argument in method char *Hex2Binary(char DE[]) – user2357643 Oct 29 '15 at 03:07

3 Answers3

1

C's switch only works with integers. In your case it looks like you can convert the switch arg to an integer:

#include <stdlib> // for strtol
/* char* is a more typical string representation than char[] */
char *Hex2Binary(char* DE)
{
    char *myBinary;
    long de_as_long = strtol(DE, NULL, 16);
    switch (de_as_long)
    {
        case 0:
         myBinary = "0000";
         break;
        /* ... */

This only works if all possible values of DE can be converted to an integer.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • I got the point to convert it into integer.But I have case "A":, case "B": ... as well.Whether I need to give ascii value for A,B.... – user2357643 Oct 29 '15 at 03:17
  • Check out http://stackoverflow.com/questions/20654424/atoi-from-hex-representation-string for the hex capable version of atoi (answer edited to use it) – John3136 Oct 29 '15 at 03:21
0

The switch() will accept only integer.

You have to pass the integer for switch not a character array.

DE is a character array. Change that to give which position or integer you want to pass to the switch.

0

C doesn't let you switch on char arrays or strings, but it does let you switch on chars:

switch(DE[0]) {
case '0':
    myBinary = "0000";
    break;
case '1':
    myBinary = "0001";
    break;
/* ... */
}

but really, there's a much simpler and more concise way to do this:

long val = strtol(DE, NULL, 16);
char myBinary[5] = {"01"[(val >> 3) & 1],
                    "01"[(val >> 2) & 1],
                    "01"[(val >> 1) & 1],
                    "01"[val & 1],
                    '\0' /* null terminator */
};

and you can use a loop to simplify this further.

nneonneo
  • 171,345
  • 36
  • 312
  • 383