Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

I'm working on a program to convert decimal to binary but I don't understand so well the part under the main and why an error occurs in the line for(res=7;;) could anyone explain me this? Please, thanks.

This is the program:

#include <stdio.h>

#include <conio.h>

binary(int num, int arr[8])

{

int base=2, temp, i=7;

num=10;

while(base>=num)

{

temp=num/base;

digit=num%base;

num=temp;

arr[i]=digit;

i--;

}

arr[i]=num;

}

main()

{

int res[8];

binary(10,res);

for(res=7;;);

printf("%d",res);

getch();

}

This is the program:

#include <stdio.h>

#include <conio.h>

binary(int num, int arr[8])

{

int base=2, temp, i=7;

num=10;

while(base>=num)

{

temp=num/base;

digit=num%base;

num=temp;

arr[i]=digit;

i--;

}

arr[i]=num;

}

main()

{

int res[8];

binary(10,res);

for(res=7;;);

printf("%d",res);

getch();

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

But for now, I'd like you to try a bit to find what are the logical problems in the binary() function. Please try, and if you're still stuck, paste your new code and we'll discuss it.

Let's take a decimal number : 1243. It's representation actually means :

1 x 10^3 + 2 x 10^2 + 4 x 10^1 + 3 x 10^0

So, if we would consider powers of ten as "digits", that would give :

digit 3 : 1

digit 2 : 2

digit 1 : 4

digit 0 : 3

The formula to retrieve this "mapping" is simple and looks like the alogrythm in your binary() function

1243 / 10 = 124 --> use this for next loop

1243 % 10 = 3 --> "bit" 0 is 3

124 / 10 = 12 --> use this for next loop

124 % 10 = 4 --> "bit" 1 is 4

12 / 10 = 1 --> use this for next loop

12 % 10 = 4 --> "bit" 2 is 2

1 / 10 = 0 --> that's the last loop

1 % 10 = 1 --> "bit" 3 is 1

The same algorythm can be applied for any base, 2 for instance.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial#include <conio.h>

void binary(int num, int arr[8])

{

int base=2, i;

for(i=0; i<8; i++)

{

arr[i]=num%base;

num=num/base;

}

}

int main()

{

int i, res[8];

binary(10,res);

for(i=7; i>=0; i--)

printf("%d",res[i]);

getch();

return 0;

}

Good bye.

#include<stdio.h>

#include<conio.h>

typedef struct byte

{

unsigned int fi : 1; //First bit denoted by fi.

unsigned int se : 1; //Second bit denoted by se.

unsigned int th : 1; //Third bit denoted by th.

unsigned int fo : 1; //Fourth bit denoted by fo.

unsigned int ff : 1; //Fifth bit denoted by ff.

unsigned int si : 1; //Sixth bit denoted by si.

unsigned int sv : 1; //Seventh bit denoted by sv.

unsigned int ei : 1; //Eighth bit denoted by ei.

} byte;

byte test;

void byte_print(byte);

void convert_byte(int);

void main()

{

int a;

// struct byte test;

printf("Enter the number < 255 to be converted to binary");

scanf("%d", &a);

convert_byte(a);

byte_print(test);

getch();

}

void convert_byte(int x)

{

int i;

// extern byte test;

test.fi = x & 1;

x >>= 1;

test.se = x & 1;

x >>= 1;

test.th = x & 1;

x >>= 1;

test.fo = x & 1;

x >>= 1;

test.ff = x & 1;

x >>= 1;

test.si = x & 1;

x >>= 1;

test.sv = x & 1;

x >>= 1;

test.ei = x & 1;

x >>= 1;

}

void byte_print(byte x)

{

printf("\n%d",x.ei);

printf("%d",x.sv);

printf("%d",x.si);

printf("%d",x.ff);

printf("%d",x.fo);

printf("%d",x.th);

printf("%d",x.se);

printf("%d\n",x.fi);

}

C

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

res=7

you're trying to assign an integer (7) to an array of integers (res), and the compiler isn't happy...

Actually I think that what you want is rather something like :

main()

{

int i, res[8];

binary(10,res);

for(i=7; i>0; i--)

printf("%d",res[i]);

getch();

}

Can you explain the changes I made ?