-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_experimentation.c
132 lines (115 loc) · 5.02 KB
/
Binary_experimentation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
File: Binary_experimentation.c
Purpose: To generate 8-bit binary files from sting input of the command line
Author: Adin Mauer
Date: March 2022
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// if you want copy the functions that generate the 8-bit unsigned character
// copt the code between the lines. Acces that code with in your program like this:
// unsigned char byte;
// printBinaryInput(byte);
// and your 8-bit unsigned char will be inside 'byte'
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
// converts string of 8 1's and 0's to an integer
int binString2int(char* op){
int byte = 0;
for(int i = 7 ; i >= 0 ; i--){
byte += ((op[i]-'0')*pow(2,7-i));
}
return byte ;
}
// checke that the input string fulfills 2 conditions
// (1) length of 8 characters
// (2) that all characters are only 1's or 0's
// if they conditions do not hold, the byte will be set to 0x00
// by default and an error message will be printed
int checkIf8binInput(char* op){
int onlyBin = 1;
int length = (strlen(op) == 8);
for(int i = 0 ; i < strlen(op) ; i ++)
if(!(op[i]-'0' == 0 || op[i]-'0' == 1))
onlyBin = 0;
if(onlyBin && length)
return 1;
printf("Invalid input!\nEnter a number that");
if(onlyBin && !length)
printf(" is of length 8!\n");
else if(!onlyBin && length)
printf(" only has 1's and 0's!\n");
else
printf(" has only 0's and 1's AND is of length 8!\n");
printf("\n--------------------------------------------------------------------------------------------------");
return 0;
}
//reads a string input from the command line
//and calls the check function
//then call the function that converts the string to an unsigned char
// and returns that unsigned char to the callee
unsigned char readBinaryString(){
printf("Enter a 8 bit number an input: \n");
unsigned char byte;
char op[9];
scanf("%8s", op);
if(!checkIf8binInput(op)){
return 0;
}
byte = binString2int(op);
return byte;
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
// converts a unsigned character to an 8 character string of 1's and 0's that corresponds
// to the inputed 8-bit unsigned char
char* char2BinString(unsigned char byte, char* op){
int a = 0;
for(unsigned char i = 0x01; i != 0X00 ; i = i << 1 , a++){
if((byte & i) == 0x00)
op[7-a] = '0';
else
op[7-a] = '1';
}
op[8] = '\0';
return op;
}
//this function prints the results of the 8-bit unsigned character generation
void printBinaryInput(unsigned char byte){
char op[9] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,'\n'};
strcpy(op, char2BinString(byte, op));
printf("Generating results: \n");
printf("intput is %s\n", op);
printf("opcode in hex: 0X%x\n\n", byte);
printf("Result is stored in unsigned char byte your your usage.\n");
printf("\n--------------------------------------------------------------------------------------------------\n");
}
// signature - printed only once in the beginning of the program
void printSignature(){
printf("\n--------------------------------------------------------------------------------------------------");
printf("\n\tWELCOME to byte generation program ");
printf("\t\t Author: Adin Mauer \tDate created: March 2022 \n");
printf("--------------------------------------------------------------------------------------------------\n");
printf("This program is meant to help ");
printf("with exploring different hardware.\n\n");
printf("i.e. if there is a hardware that you dont know exactly how to use - you can \n");
printf("easily use this program to continuously input from the command line binary \n");
printf("sequnces that will be stored as unsigned integers.\n\n");
printf("All you need to do is copy the functions:\n");
printf(" - unsigned char readBinaryString()\n - int checkIf8binInput(char* op)\n - int binString2int(char* op)\n");
printf("decalre an unsigned char in your program like so: unsigned char byte; ('byte' arbitrary)\n");
printf("and the instantiate the readBinaryString function like so: byte = readBinaryString();\n\n");
printf("Future functions - being able to continuously output a generated byte at a user specified input in micro-seconds.\n");
printf("\n--------------------------------------------------------------------------------------------------\n\n");
}
int main(void) {
printSignature();
unsigned char byte;
while(1){
byte = readBinaryString();
printBinaryInput(byte);
}
return 0;
}