title: How do I convert from unsigned char to long
body: <p>I have a problem to convert from unsigned char into long.</p>
  
  <p><strong>The mission:</strong> I have <code>25</code> in <code>(unsigned char) ptr-&gt;studentArr[i].subjectStatus</code> when <code>i = 0</code>, I go into the function <code>unsigned char fromDecToBinary(unsigned char tmpSubjectStatus)</code>, and I want in that function to get <code>unsigned long 11001</code> into variable <code>ret</code> and then <code>fprintf</code> it into <code>output.txt</code> file.</p>
  
  <p><strong>Expectation:</strong> to fprintf into the file <code>11001</code> when <code>i = 0</code>, problem: it prints <code>25</code> instead(if I use <code>fromDecToBinary</code> function, it prints <code>0</code>).</p>
  
  <p><strong>Please,</strong> just look at the 2 functions: <code>outPutStudents</code> and <code>fromDecToBinary</code>, other functions work properly, and those other functions just get the information and store the info. into structures which are then used to print the details into <code>output.txt</code>, most of them work, except the binary thingy.</p>
  
  <p><strong>input.txt file:</strong></p>
  
  <pre><code>Nir 32251 99.80 11001
  Ely 12347 77.89 01111
  Moshe 45321 50.34 11111
  Avi 31456 49.78 00011
  </code></pre>
  
  <p><strong>*NOTE:</strong> this is the output without using the function <code>fromDecToBinary</code>
  <strong>output.txt file:</strong></p>
  
  <pre><code>Student 1: Nir 32251 99.80 25  
  Student 2: Ely 12347 77.89 15  
  Student 3: Moshe 45321 50.34 31  
  Student 4: Avi 31456 49.78 3  
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code> #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  
  typedef struct Student{
      char* studentName; //Dyn. alloc. of stud. name
      long id; // ID Number
      float mark; // mark
      unsigned char subjectStatus;
  }Student;
  
  typedef struct University{
      Student* studentArr; // Dync. Alloc(Realloc) of students
      int numOfStudents; //num of students
  }University;
  
  void getStudents(University *ptr);
  unsigned char stringToBinary(unsigned char tmpSubjectStatus[]);
  void outPutStudents(University *ptr);
  unsigned char fromDecToBinary(University *ptr);
  
  void main()
  {
      printf("Please enter details of student: (a)");
      University uni;
      getStudents(&amp;uni); //Send address of structure University, because we want to change it not make a local copy of it
      outPutStudents(&amp;uni);
      getch();
  }
  void getStudents(University *ptr)
  {
      FILE *op;
      char tmpStudentName[20];
      long tmpId;
      float tmpMark;
      char tmpSubjectStatus[6];
      ptr-&gt;numOfStudents = 0;
      if ((op = fopen("input.txt", "r")) == NULL)
      {
          printf("Failed to open file.");
      }
      ptr-&gt;studentArr = (Student*)malloc(sizeof(Student));
      if (ptr-&gt;studentArr == NULL){
          printf("Error: memory was not allocated.");
          exit(1);
      }
      while (fscanf(op, "%s %ld %f %s", tmpStudentName, &amp;tmpId, &amp;tmpMark, tmpSubjectStatus) == 4)
      {
          ptr-&gt;numOfStudents++;
          ptr-&gt;studentArr = (Student*)realloc(ptr-&gt;studentArr, sizeof(Student) * ptr-&gt;numOfStudents); /*Additional code for Realloc fails - we didn't study!*/
          ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].studentName = (char*)malloc(sizeof(char)* strlen(tmpStudentName));
  
          if (!(ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].studentName)) //if we failed to allocate memory for studentName
          {
              while (ptr-&gt;numOfStudents &gt; 0)
              {
                  free(ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].studentName); //free  student name
                  ptr-&gt;numOfStudents--; // decrease numOfStudents by one
              }
              free(ptr-&gt;studentArr); //if all student names are free, we need to free the array
              printf("Student name was not allocated.");
              exit(1);
          }
  
          strcpy(ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].studentName, tmpStudentName);
          ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].id = tmpId;
          ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].mark = tmpMark;
          ptr-&gt;studentArr[ptr-&gt;numOfStudents - 1].subjectStatus = stringToBinary(tmpSubjectStatus); //atoi: from "11001"(string) to 11001(int),then casting to unsigned char
      }
  
      fclose(op);
  }
  void outPutStudents(University *ptr)
  {
      int i;
      FILE *fp;
      unsigned char tmpSubjectStatus;
      long val;
      if ((fp = fopen("output.txt", "w")) == NULL)
      {
          printf("Couldn't open output file.");
          exit(1);
      }
      for (i = 0; ptr-&gt;numOfStudents != i; i++){
          tmpSubjectStatus = ptr-&gt;studentArr[i].subjectStatus;
          val = fromDecToBinary(tmpSubjectStatus);
          fprintf(fp, "Student %d: %s %ld %.2f %ld  \n", i + 1, ptr-&gt;studentArr[i].studentName, ptr-&gt;studentArr[i].id, ptr-&gt;studentArr[i].mark, tmpSubjectStatus);
      }
      fclose(fp);
  }
  
  unsigned char stringToBinary(char tmpSubjectStatus[])
  {
      unsigned char tmpBinaryCh = 0;
      int i;
      for (i = 0; i &lt; 5; i++){
          if (tmpSubjectStatus[i] == '1') tmpBinaryCh += 1 &lt;&lt; (4 - i);
      }
  
      return tmpBinaryCh;
  }
  
  unsigned char fromDecToBinary(unsigned char tmpSubjectStatus)
  {
      int i;
      long ret;
      char arrBinary[6];
      for (i = 0; i &lt; 5; i++){
          arrBinary[4 - i] = tmpSubjectStatus % 2;
          tmpSubjectStatus /= 2;
      }
      arrBinary[5] = '/0';
  
      ret = strtol(arrBinary, NULL, 10);
      return ret;
  }
  </code></pre>