[英]Word to binary numbers program in C
I am trying to make program that converts word to binary numbers. 我正在尝试制作将单词转换为二进制数字的程序。 For Example:
例如:
Input: 输入:
abcd
A B C D
Output(Firstly the ascii code of a,b,c and d): 输出(首先是a,b,c和d的ASCII码):
(ASCII CODE of a,b,c and d)
(a,b,c和d的ASCII码)
a: 97
一个:97
b: 98
b:98
c: 99
c:99
d: 100
d:100
Than convert these to binary:
比将它们转换为二进制:
97: 1100001
97:1100001
98: 1100010
98:1100010
99: 1100011
99:1100011
100: 1100100
100:1100100
And finally the output should be like this:
最后,输出应如下所示:
110000|11100010|1100011|1100100
110000 | 11100010 | 1100011 | 1100100
Or without the straight lines between them.
或者它们之间没有直线。
But i am getting an error : 但是我遇到一个错误:
Input: 输入:
a
一种
Output: 输出:
Segmentation failed(core dumped)
细分失败(核心已转储)
Here is the code: 这是代码:
#include <stdio.h>
#define MAXINPUTCHAR 100
#define MAXBINARYNUMERAL 32
int thestr[MAXINPUTCHAR];
int theastr[MAXINPUTCHAR];
int thebstr[MAXINPUTCHAR][MAXBINARYNUMERAL];
//function declerations...
void mydtob(int,int*);//Decimal to binary converter
void mystreverse(int*);//Reverse the string
void mycopy(int*,int*);//Copy array into array
void mystoa(int*,int*);//Char array to ascii codes of the chars array
void mydtobhelper(int*,int [MAXINPUTCHAR][MAXBINARYNUMERAL]);//The function that loops through and calls mydtob
void mygetline(int*);
void printArray(int [MAXINPUTCHAR][MAXBINARYNUMERAL]);//Print 2D array
void mymdcp(int target[MAXINPUTCHAR][MAXBINARYNUMERAL],int* from,int targetIndex);//Copy array to 2D array
int main(void) {
mygetline(thestr);
mystoa(thestr,theastr);
mydtobhelper(theastr,thebstr);
printArray(thebstr);
return 0;
}
void mydtobhelper(int* decimal,int target[MAXINPUTCHAR][MAXBINARYNUMERAL]){
int singlenumbinary[MAXBINARYNUMERAL];
for(int i=0;decimal[i];i++){
mydtob(decimal[i],singlenumbinary);
mymdcp(target,singlenumbinary,0);
target[i+1][0]='\0';
}
}
void mydtob(int decimal,int* target){
int base=2;
int quotient=decimal;
int remainder=0;
int i=0;
while(quotient!=0){
remainder=quotient%base;
quotient=quotient/base;
target[i]=remainder+'0';
target[i+1]='\0';
i++;
}
mystreverse(target);
}
void printArray(int arraytoprint[MAXINPUTCHAR][MAXBINARYNUMERAL]){
char convertedarr[MAXINPUTCHAR][MAXBINARYNUMERAL];
for(int i=0;i<MAXINPUTCHAR;i++) {
for(int k=0;i<MAXBINARYNUMERAL;k++){
convertedarr[i][k]=arraytoprint[i][k];
}
}
for(int i=0;convertedarr[i][0];i++){
printf("%s",convertedarr[i]);
}
}
void mystreverse(int* str){
int copiedstr[MAXINPUTCHAR];
int i=0;
for(i=0;str[i];i++);
i--;
mycopy(str,copiedstr);
for (int k=i;k>=0;k--){
str[k]=copiedstr[i-k];
}
str[i+1]='\0';
}
void mycopy(int* from,int* target){
for(int i=0;from[i];i++){
target[i]=from[i];
target[i+1]='\0';
}
}
void mystoa(int* str,int* target) {
for (int i = 0; str[i];i++) {
//printf("%d\n",i);
if (str[i] >= 'a' && str[i] <= 'z' || str[i]==' ' || str[i]=='\t') {
int n = str[i];
target[i]=n;
target[i+1]='\0';
}
}
}
void mygetline(int* target){
int i=0;
int c=0;
while((c=getchar())!=EOF && c != '\n'){
target[i]=c;
target[i+1]='\0';
i++;
}
}
void mymdcp(int target[MAXINPUTCHAR][MAXBINARYNUMERAL], int* from, int targetIndex) {
for (int j = 0; j < from[j]; j++) {
target[targetIndex][j] = from[j];
}
}
The segmentation fault is being caused by a faulty loop in the function printArray()
: 分段错误是由函数
printArray()
的错误循环引起的:
for(int k=0;i<MAXBINARYNUMERAL;k++){
convertedarr[i][k]=arraytoprint[i][k];
The i
in the loop condition should be a k
. 循环条件中的
i
应该是k
。 When this is fixed, the program works: 解决此问题后,程序将运行:
λ> ./a.out
z
1111010
λ> ./a.out
a
1100001
λ> ./a.out
f
1100110
I think that code readability may have been an issue here. 我认为代码的可读性可能是这里的问题。 Consider the code as it was originally formatted:
考虑原始格式的代码:
void printArray(int arraytoprint[MAXINPUTCHAR][MAXBINARYNUMERAL]){
char convertedarr[MAXINPUTCHAR][MAXBINARYNUMERAL];
for(int i=0;i<MAXINPUTCHAR;i++) {
for(int k=0;i<MAXBINARYNUMERAL;k++){
convertedarr[i][k]=arraytoprint[i][k];
}
}
for(int i=0;convertedarr[i][0];i++){
printf("%s",convertedarr[i]);
}
putchar('\n');
}
compared with this: 与此相比:
void printArray(int arraytoprint[MAXINPUTCHAR][MAXBINARYNUMERAL])
{
char convertedarr[MAXINPUTCHAR][MAXBINARYNUMERAL];
for (int i = 0;i < MAXINPUTCHAR; i++) {
for (int k = 0;i < MAXBINARYNUMERAL; k++){
convertedarr[i][k] = arraytoprint[i][k];
}
}
for (int i = 0;convertedarr[i][0]; i++){
printf("%s", convertedarr[i]);
}
putchar('\n');
}
Both of these snippets contain the original error, but the second one makes it just a little easier to spot, since the symbols aren't quite as crammed together. 这两个代码片段都包含原始错误,但是第二个代码片段却使它更容易发现,因为这些符号并没有完全塞在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.