-2

I am using below function to convert Decimal to binary

char** DEtoBinary(char HexDE[])
{
    printf("HexDE = %s\n", HexDE);
    int I;
    char* deBinary[16];
    for (I = 0; I <= 15; I++)
    {
        //deBinary = deBinary + Hex2Binary(HexDE.Substring(I, 1));
        deBinary[I] = strcpy(deBinary, Hex2Binary(substring_added1(HexDE, I, 1)));

    }
    printf("deBinary = %s\n", deBinary);
    return deBinary;

}

Hex to binary function

char *Hex2Binary(char* DE)
{
    printf("Inside DE = %s\n", DE);

    char *myBinary;
    long val = strtol(DE, NULL, 16);

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

        case 1:
         myBinary = "0001";
         break;

        case 2:
         myBinary = "0010";
         break;

        case 3:
         myBinary = "0011";
         break;

        case 4:
         myBinary = "0100";
         break;

        case 5:
         myBinary = "0101";
         break;

        case 6:
         myBinary = "0110";
         break;

        case 7:
         myBinary = "0111";
         break;

        case 8:
         myBinary = "1000";
         break;

        case 9:
         myBinary = "1001";
         break;

        case 10: //A
         myBinary = "1010";
         break;

        case 11: //B
         myBinary = "1011";
         break;

        case 12://C
         myBinary = "1100";
         break;

        case 13://D
         myBinary = "1101";
         break;

        case 14://E
         myBinary = "1110";
         break;

        case 15: //F
         myBinary = "1111";
         break;

    }
    printf("myBinary = %s\n" ,myBinary);
    return myBinary;
 }

In Hex2Binary function, myBinary is returning properly, but I need to send whole binary converted string to original caller of char* DEtoBinary(char HexDE[])

original caller is

de1Binary = DEtoBinary(DE[0]);

Example my DE[0] = E234567787888888

Expected is 111000100011........... But I am getting only binary value of last hex value i.e. 8 is 1000

user2357643
  • 69
  • 1
  • 1
  • 5
  • 2
    The code for DEtoBinary shall not even compile. Your function return type is `char*` and you are returning a `char**` – user007 Oct 29 '15 at 13:27
  • Your posted code contains too many inconsistencies like `char **` of Hex2Binary which returns `char*`, `deBinary[I] = strcpy` and `strcpy(char**, char**)`;. Please post the actual code. I don't understand how comes that it works for you in any case. – Alex Lop. Oct 29 '15 at 13:29
  • It is compiling. But wrong from my side.I corrected to char*.I will edit in my original post – user2357643 Oct 29 '15 at 13:31
  • Have you put the warnings on. There are so may bugs in this code. Use of `strcpy`. Returning stuff that is one the stack and therefore goes out of scope.... – Ed Heal Oct 29 '15 at 13:36
  • 1
    Voted to close. Post something we can compile. – Karoly Horvath Oct 29 '15 at 13:37
  • 1
    I think there is some problem with your compiler, if you can post all of your code on ideone.com and if it works then share the link as well, I guess – user007 Oct 29 '15 at 13:40
  • I written all code above in my original post. My DEtoBinary function need to return complete binary string in DE[0] = "1110", DE[1] = "0010" .... – user2357643 Oct 29 '15 at 13:49
  • @user2357643 my very first comment still holds true.`deBinary` is an array of pointers and hence a of type `char**` and the original function has the return type as `char*` hence this doesn't compile. – user007 Oct 29 '15 at 13:52
  • @user007 I corrected that mistake still same problem – user2357643 Oct 29 '15 at 14:01
  • http://ideone.com/LLZwaA read the last error in the link. – user007 Oct 29 '15 at 14:03
  • I corrected in my original post as well my code in my system.Still getting same problem. – user2357643 Oct 29 '15 at 14:08

2 Answers2

0

Few things which I still see wrong are ::

deBinary[I] = strcpy(deBinary, Hex2Binary(substring_added1(HexDE, I, 1)));

You are again copying a char* to a char** which is not a good idea. Probably you shall try something like ::

strcpy(deBinary[I], Hex2Binary(substring_added1(HexDE, I, 1)));

Check this :: strcpy() return value

So, I don't think there is any need to save the value returned by strcpy.

Moreover, in your print statement you print

printf("deBinary = %s\n", deBinary);

where deBinary is again a char** :P But, from what I understand, that it only prints first 4 binary digits is because it encounters \0 at the end of the first binary representation.

So, probably you shall try to do::

for(int i = 0; i < 16; i++) {
    printf("%s ", deBinary[i]);
}

This, might actually solve your problem.

Moreover, in your code, you declare something like this, char* deBinary[16];, which is an array of 16 char* which are meant to store the address of 16 char arrays, but infact you use strcpy to copy the string returned from Hex2Binary to the location pointed by deBinary[i], which has not been allocated any memory, so you are copying characters to memory you never allocated, which is a bad idea (but might work on your device), so it is advisable either you declare your deBinary as char deBinary[16][5] or, instead of using strcpy, simply copy the address returned by Hex2Binary like

deBinary[i] = Hex2Binary( ... );

Because as far as I know, when you declare something like "1100" to a char* that is allocated to the constant memory, and might work. (I am not sure about this, if someone can comment about this and help it would be great).

I wish this could help!

EDIT ::

Moreover, talking again about the deBinary[], you have locally declared a char** and returning it from the function, which is wrong since after a function call gets over, all the variables declared are scrapped since the function call gets out of the stack! So, if you do not need the converted array in the main, you can try changing the type of the function to void. Or else, try dynamically allocating deBinary or else allocating itself in the main and passing it as a parameter to the function DEtoBinary.

Community
  • 1
  • 1
user007
  • 2,156
  • 2
  • 20
  • 35
-1

You have confused something about char * and char **; The return type of Hex2Binary(char*) is char **, but you return a char *; The return type of DEtoBinary(char[]) is char *, but you return a char **;

Remember:

char *val is same as char valp[];

char *val[] is same as char **val;

Yinuo
  • 36
  • 5