2016-04-23 36 views
0

我有一個問題,從無符號字符轉換爲長。如何從無符號字符轉換爲長

使命:25(unsigned char) ptr->studentArr[i].subjectStatusi = 0,我進入功能unsigned char fromDecToBinary(unsigned char tmpSubjectStatus),我想在函數獲取unsigned long 11001爲可變ret然後fprintfoutput.txt文件。

後市展望:到fprintf中到文件11001i = 0,提出問題:它打印25代替(如果我使用fromDecToBinary功能,它打印0)。

只看2個功能:outPutStudentsfromDecToBinary,其他功能正常工作,而那些其它功能只是獲取信息並存儲信息。到結構中,然後用於將細節打印到output.txt中,除了二進制東西之外,它們中的大多數都可以工作。

input.txt的文件:

Nir 32251 99.80 11001 
Ely 12347 77.89 01111 
Moshe 45321 50.34 11111 
Avi 31456 49.78 00011 

*注:這是不使用函數的輸出fromDecToBinary output.txt的文件:

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 

代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 

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(&uni); //Send address of structure University, because we want to change it not make a local copy of it 
    outPutStudents(&uni); 
    getch(); 
} 
void getStudents(University *ptr) 
{ 
    FILE *op; 
    char tmpStudentName[20]; 
    long tmpId; 
    float tmpMark; 
    char tmpSubjectStatus[6]; 
    ptr->numOfStudents = 0; 
    if ((op = fopen("input.txt", "r")) == NULL) 
    { 
     printf("Failed to open file."); 
    } 
    ptr->studentArr = (Student*)malloc(sizeof(Student)); 
    if (ptr->studentArr == NULL){ 
     printf("Error: memory was not allocated."); 
     exit(1); 
    } 
    while (fscanf(op, "%s %ld %f %s", tmpStudentName, &tmpId, &tmpMark, tmpSubjectStatus) == 4) 
    { 
     ptr->numOfStudents++; 
     ptr->studentArr = (Student*)realloc(ptr->studentArr, sizeof(Student) * ptr->numOfStudents); /*Additional code for Realloc fails - we didn't study!*/ 
     ptr->studentArr[ptr->numOfStudents - 1].studentName = (char*)malloc(sizeof(char)* strlen(tmpStudentName)); 

     if (!(ptr->studentArr[ptr->numOfStudents - 1].studentName)) //if we failed to allocate memory for studentName 
     { 
      while (ptr->numOfStudents > 0) 
      { 
       free(ptr->studentArr[ptr->numOfStudents - 1].studentName); //free student name 
       ptr->numOfStudents--; // decrease numOfStudents by one 
      } 
      free(ptr->studentArr); //if all student names are free, we need to free the array 
      printf("Student name was not allocated."); 
      exit(1); 
     } 

     strcpy(ptr->studentArr[ptr->numOfStudents - 1].studentName, tmpStudentName); 
     ptr->studentArr[ptr->numOfStudents - 1].id = tmpId; 
     ptr->studentArr[ptr->numOfStudents - 1].mark = tmpMark; 
     ptr->studentArr[ptr->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->numOfStudents != i; i++){ 
     tmpSubjectStatus = ptr->studentArr[i].subjectStatus; 
     val = fromDecToBinary(tmpSubjectStatus); 
     fprintf(fp, "Student %d: %s %ld %.2f %ld \n", i + 1, ptr->studentArr[i].studentName, ptr->studentArr[i].id, ptr->studentArr[i].mark, tmpSubjectStatus); 
    } 
    fclose(fp); 
} 

unsigned char stringToBinary(char tmpSubjectStatus[]) 
{ 
    unsigned char tmpBinaryCh = 0; 
    int i; 
    for (i = 0; i < 5; i++){ 
     if (tmpSubjectStatus[i] == '1') tmpBinaryCh += 1 << (4 - i); 
    } 

    return tmpBinaryCh; 
} 

unsigned char fromDecToBinary(unsigned char tmpSubjectStatus) 
{ 
    int i; 
    long ret; 
    char arrBinary[6]; 
    for (i = 0; i < 5; i++){ 
     arrBinary[4 - i] = tmpSubjectStatus % 2; 
     tmpSubjectStatus /= 2; 
    } 
    arrBinary[5] = '/0'; 

    ret = strtol(arrBinary, NULL, 10); 
    return ret; 
} 
+1

你不能把11001放到'unsigned char'中 - 在幾乎所有的計算機硬件上,'unsigned char'的最大可能值是255. –

+0

@BillyONeal我明白了,我如何將unsigned char'25'轉換爲打印那麼長'11001? –

+0

你不能。這就像說「我怎麼把5加侖的牛奶放入1加侖的牛奶罐裏」 –

回答

1

你必須在fromDecToBinary功能的幾個誤區:

  • 更換'/0''\0'
  • '0' + tmpSubjectStatus % 2存儲在數組中。
  • strtol調用添加適當的錯誤處理。
  • 將退貨類型更改爲long
+0

不,仍然'ret = strtol(arrBinary,NULL,10)'中的'ret'值爲零,這就是爲什麼它不會'你知道爲什麼它是這樣的嗎? –

+0

上帝保佑你,它像一個魅力 –

+0

我不知道'0'+ tmpSubjectStatus%2' –

0

如果要使用數字打印一些二進制數,請使用此選項。

#include <stdio.h> 
#include <stdlib.h> 

void print_bin(uint64_t num, size_t bytes) { 
    int i = 0; 
    for(i = bytes * 8; i > 0; i--) { 
    (i % 8 == 0) ? printf("|") : 1; 
    (num & 1) ? printf("1") : printf("0"); 
    num >>= 1; 
    } 
    printf("\n"); 
} 

int main(void) { 
    int arg = atoi("25"); 
    print_bin(arg, 1); 
    return 0; 
} 

它還會每8位打印一個垂直條以使字節更容易閱讀,但您可以將其刪除。

如果要指定要多少字節使用

#include <stdio.h> 
#include <stdlib.h> 

void print_bin(uint64_t num, size_t bytes) { 
    int i = 0; 
    for(i = bytes * 8; i > 0; i--) { 
    (i % 8 == 0) ? printf("|") : 1; 
    (num & 1) ? printf("1") : printf("0"); 
    num >>= 1; 
    } 
    printf("\n"); 
} 

int main(void) { 
    print_bin(16000, 3); 
    return 0; 
} 
+0

The strlen of th二進制字符必須是5,如輸入。並且我已經使用函數'fromDecToBinary'實現了從十進制到二進制的轉換。 –

+0

讓我改變它以使它變成十進制。兩秒 – Harry

+0

哈利,如果你可以改變我寫的函數,那麼我會非常感謝,我真的不明白我如何在我的代碼中實現你的函數並使它工作。 ( –

0
#include <stdio.h> 

int main() 
{ 
unsigned char tmpSubjectStatus=25; 
long quotient = tmpSubjectStatus; 
long remainder; 
long binary=0; 
long multiplier=1; 

while(quotient!=0){ 
    remainder=quotient % 2; 
    binary=binary+(remainder*multiplier); 
    multiplier=multiplier*10; 
    quotient = quotient/2; 
} 
    printf("%ld",binary); 

return 0; 
} 

試試這個。 在功能就會像這樣

long fromDecToBinary(unsigned char tmpSubjectStatus) 
{ 
long quotient = tmpSubjectStatus; 
long remainder; 
long binary=0; 
long multiplier=1; 

while(quotient!=0){ 
    remainder=quotient % 2; 
    binary=binary+(remainder*multiplier); 
    multiplier=multiplier*10; 
    quotient = quotient/2; 
} 

return binary; 
} 

在這裏返回類型更改爲長。

+0

nope。'學生1: Nir 32251 99.80 249' –

相關問題