Question

I need little help with C language. I need to pass what I get from HexToBin(char*...

I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr.
Example:
Enter IEEE-Hex: 40200000
Equivalent Binary value is : 01000000001000000000000000000000
Decimal Number: 2.5

#include <stdio.h>

void HexToBin(char* hexdec)
{

   long int i = 0;

   while (hexdec[i]) {

       switch (hexdec[i]) {
       case '0':
           printf("0000");
           break;
       case '1':
           printf("0001");
           break;
       case '2':
           printf("0010");
           break;
       case '3':
           printf("0011");
           break;
       case '4':
           printf("0100");
           break;
       case '5':
           printf("0101");
           break;
       case '6':
           printf("0110");
           break;
       case '7':
           printf("0111");
           break;
       case '8':
           printf("1000");
           break;
       case '9':
           printf("1001");
           break;
       case 'A':
       case 'a':
           printf("1010");
           break;
       case 'B':
       case 'b':
           printf("1011");
           break;
       case 'C':
       case 'c':
           printf("1100");
           break;
       case 'D':
       case 'd':
           printf("1101");
           break;
       case 'E':
       case 'e':
           printf("1110");
           break;
       case 'F':
       case 'f':
           printf("1111");
           break;
       default:
           printf("\nInvalid hexadecimal digit %c",
               hexdec[i]);
       }
       i++;
   }
}


union {
int i;
float f;
} myunion;

int binstr2int(char *s)
{
int rc;
for (rc = 0; '\0' != *s; s++) {
if ('1' == *s) {
rc = (rc * 2) + 1;
} else if ('0' == *s) {
rc *= 2;
}
}
return rc;
}

int main(void) {
printf("\nEnter IEEE-Hex: ")
char hexdec[100];
   scanf("%s", &hexdec);
   printf("\nEquivalent Binary value is : ");
   HexToBin(hexdec); // this is printing Binary numbers

// I need to pass HexToBin() into char* input so that it will contain binary string array;

char * input = // if you will put here something like "111100001111"// it will give you decimal numbers // I want to pass here what I get from HexToBin(hexdec) and it should be in string array;
float *output;


int converted = binstr2int(input);

output = (float*)&converted; //
printf("\nDecimal Number: %f\n", *output); // this is what printing decimal number numbers
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/********************************main.c****************/

#include <stdio.h>
#include <string.h>
char* HexToBin(char* hexdec)
{

long int i = 0;
char binayString[100] = "";
while (hexdec[i]!='\0') {

switch (hexdec[i]) {
case '0':
strcat(binayString, "0000");
break;
case '1':
strcat(binayString, "0001");
break;
case '2':
strcat(binayString, "0010");
break;
case '3':
strcat(binayString, "0011");
break;
case '4':
strcat(binayString, "0100");
break;
case '5':
strcat(binayString, "0101");
break;
case '6':
strcat(binayString, "0110");
break;
case '7':
strcat(binayString, "0111");
break;
case '8':
strcat(binayString, "1000");
break;
case '9':
strcat(binayString, "1001");
break;
case 'A':
case 'a':
strcat(binayString, "1010");
break;
case 'B':
case 'b':
strcat(binayString, "1011");
break;
case 'C':
case 'c':
strcat(binayString, "1100");;
break;
case 'D':
case 'd':
strcat(binayString, "1101");
break;
case 'E':
case 'e':
strcat(binayString, "1110");
break;
case 'F':
case 'f':
strcat(binayString, "1111");
break;
default:
printf("\nInvalid hexadecimal digit %c",
hexdec[i]);
}
i++;
}
printf("%s ",binayString);
return binayString;
}


union {
int i;
float f;
} myunion;

int binstr2int(char* s)
{
int rc;
for (rc = 0; '\0' != *s; s++) {
if ('1' == *s) {
rc = (rc * 2) + 1;
} else if ('0' == *s) {
rc *= 2;
}
}
return rc;
}

int main(void) {
   printf("\nEnter IEEE-Hex: ");
   char hexdec[100];
    scanf("%s", &hexdec);
    printf("\nEquivalent Binary value is: ");
// this is printing Binary numbers
  
// I need to pass HexToBin() into char* input so that it will contain binary string array;

char * input = HexToBin(hexdec);// if you will put here something like "111100001111"// it will give you decimal numbers // I want to pass here what I get from HexToBin(hexdec) and it should be in string array;
float *output;


int converted = binstr2int(input);

output = (float*)&converted; //
printf("\nDecimal Number: %f\n", *output); // this is what printing decimal number numbers
}

/**********************output******************/

Enter IEEE-Hex: 40200000

Equivalent Binary value is: 01000000001000000000000000000000
Decimal Number: 2.500000

--------------------------------

Please let me know if you have any doubt or modify the answer, Thanks:)

Add a comment
Know the answer?
Add Answer to:
I need little help with C language. I need to pass what I get from HexToBin(char*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • This code in c evaluates a mathematical expression and checks that it is correctly balanced, then...

    This code in c evaluates a mathematical expression and checks that it is correctly balanced, then converts the expression to its postfix form (This part works). Now I need to evaluate the postfix expression with a function. I've been compiling it using dev c ++. #include #include #define SIZE 20 #include int profundidad(char expresion[]); int prec(char op1, char op2); void postfijo(char expresion[]); char funcion[SIZE]; float convierte(char car); float resultado(float opnd1, char symb, float opnd2); float evaluar(char expresion[]); #define SIZE 20...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT