0

I need to make a program that resolve operations between hexadecimal, where i ask for the number of hexadecimals to operate, ask for them, and then show the result.

The operations that can be done are or,and,xor(|,^,&). I made the convert part with arrays where i storage the hex number, and then convert those in binary and save it in another array but when i realize i coudnt know how many integers of hex were going to operate i coudnt figure out how to create enough arrays to storage those someone told me about i had to use pointers. Im new to that and research a bit about that still dont know how pointers can work that out...

Part of my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
int main () {
  char op,bin1[31]="",bin2[31]="",hex1[100],hex2[100];
  int sizeh,repeat1,repeat2,n,z,i;
              printf("Write Hexadecimal:);
              scanf("%s",hex1);
              convert(hex1,bin1,n);

function convert:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
void convert(char hex1[],char bin1[],int n){
int i,b;
printf("\nEquivalent binary value: ");
for(i=0;i<4;i++){
switch(hex1[i]){
case '0': strcat(bin,"0000"); break;
case '1': strcat(bin,"0001"); break;
case '2': strcat(bin,"0010"); break;
case '3': strcat(bin,"0011"); break;
case '4': strcat(bin,"0100"); break;
case '5': strcat(bin,"0101"); break;
case '6': strcat(bin,"0110"); break;
case '7': strcat(bin,"0111"); break;
case '8': strcat(bin,"1000"); break;
case '9': strcat(bin,"1001"); break;
case 'A': strcat(bin,"1010"); break;
case 'B': strcat(bin,"1011"); break;
case 'C': strcat(bin,"1100"); break;
case 'D': strcat(bin,"1101"); break;
case 'E': strcat(bin,"1110"); break;
case 'F': strcat(bin,"1111"); break;
case 'a': strcat(bin,"1010"); break;
case 'b': strcat(bin,"1011"); break;
case 'c': strcat(bin,"1100"); break;
case 'd': strcat(bin,"1101"); break;
case 'e': strcat(bin,"1110"); break;
case 'f': strcat(bin,"1111"); break;
default:  printf("\nInvalid hexadecimal digit %c ",hex[i]);
}
}
printf("%s",bin);
}
bronze56k
  • 55
  • 1
  • 6
  • So you want to read an arbitrary number of hexadecimal strings and convert them to binary strings, ok, but what is that with the operators? Can you give an example? Or is your problem the handling of an arbitrary number of hexstrings? – deamentiaemundi Jul 17 '16 at 02:35
  • Would recommend converting the string read to an integer value with `strtol` (it will handle *hex* with `base` equal `16`), then output the binary representation. Otherwise you will run into *endian* and misconversion issues attempting to convert as shown above. – David C. Rankin Jul 17 '16 at 02:41

1 Answers1

1

I'm still not sure, if you want to do it all with strings (you need to do so if you want to go very large without using big integers)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// for tolower()
#include <ctype.h>

int convert(char *hex, size_t len, char **bin)
{
  size_t i;
  char c;

  for (i = 0; i < len; i++) {
    // work on lower-case to safe us some work
    c = tolower(hex[i]);
    // TODO: use a table instead of a large switch,
    //       or a union
    switch (c) {
    case '0':
      strcat(*bin, "0000");
      break;
    case '1':
      strcat(*bin, "0001");
      break;
    case '2':
      strcat(*bin, "0010");
      break;
    case '3':
      strcat(*bin, "0011");
      break;
    case '4':
      strcat(*bin, "0100");
      break;
    case '5':
      strcat(*bin, "0101");
      break;
    case '6':
      strcat(*bin, "0110");
      break;
    case '7':
      strcat(*bin, "0111");
      break;
    case '8':
      strcat(*bin, "1000");
      break;
    case '9':
      strcat(*bin, "1001");
      break;
    case 'a':
      strcat(*bin, "1010");
      break;
    case 'b':
      strcat(*bin, "1011");
      break;
    case 'c':
      strcat(*bin, "1100");
      break;
    case 'd':
      strcat(*bin, "1101");
      break;
    case 'e':
      strcat(*bin, "1110");
      break;
    case 'f':
      strcat(*bin, "1111");
      break;
    default:
        // should not happen, an error
        return -1;
        break;
    }
  }
  return len;
}

int main()
{
  // number of inputs
  int count;
  // length of input
  int len;
  // iterator
  int i;
  // first hexadecimal string (100 characters plus '\0')
  // fixed size memory because the modifier 'm' is in Posix not standard-C
  // and scanning input manually is more complicated.
  char hex[101];
  // binary strings
  // gets allocated dynamically
  char **bin;

  puts("Number of Hexadecimal:");
  scanf(" %d", &count);
  // allocate an array for the strings
  bin = malloc(count* sizeof(char *));
  // gather input
  for(i = 0;i < count;i++){
    printf("Hexadecimal #%d:",i+1);
    scanf(" %100s", hex);
    len = strlen(hex);
    //TODO: check input for correctness
    // Hint: try isxdigit() (also in ctype.h)

    // allocate memory for the individual string in the array (8 bits in a byte)
    bin[i] = malloc(len * 8 + 1);
    bin[i][0] = '\0';
    convert(hex, len, &bin[i]);
    printf("Binary #%d: %s\n",i+1,bin[i]);
  }

  // if you want to store the individual hextrings, too, just make an
  // array "hexarray" and handle it just like the "bin" array.

  // Now all hexstrings are converted and in the array "bin"
  // Do whatever you want to do with them here

  // free memory (although not needed at the end of main, just good style)
  for(i = 0;i < count;i++){
    free(bin[i]);
  }
  free(bin);

  /*
     Testvalues:
     1234abdf -> 00010010001101001010101111011111
     abdf1234 -> 10101011110111110001001000110100
   */

  // exit and return the correct value the operating system assigned to
  // "program ended without error"
  exit(EXIT_SUCCESS);
}

Edit:

C does not assume anything, you need to tell it explicitly. So, if you want an array of something you need to tell the compiler the number of members in advance or allocate a couple of members and grow it later with realloc(), if necessary.

We know the size of the array in advance because we asked for it, hence we allocate that exact amount. But that is only the array, the content needs its own memory.

Let me try a metaphor (a simile? I*ll never know): imagine the array as a rail with a number of hooks; you might find such a thing in you kitchen. If you want to hang your spices at these hooks you cannot do it directly, you need to put the in a container that is large enough to keep the amount of spices you want to fill in. If the container is too small, it will flow over [sic!], if it is too big it is a waste.

So you need to know the minimum number of hooks (for the first malloc) and the minimum size of the container (for the second malloc). To stretch that metaphor to its limits: I used hex as a spoon to fill the containers bin[i].

If you want to have arbitrary large input: try fgets() and company.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Thanks for the answer helped me alot but i have a question. Why when ask for the number of hexadecimal u allocate the array for the string with 'bin'. – bronze56k Jul 17 '16 at 15:01