Skip to content

[feat/fix/docs]: Improved on conversion code , length variable , asserts added #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
67eeefe
Added asserts , number length check
lazy-dude Jul 8, 2021
0008c67
Remove extra empty lines
lazy-dude Jul 8, 2021
7185941
Auto indent
lazy-dude Jul 8, 2021
6e5e561
fix: corrected shown hex number.
lazy-dude Jul 8, 2021
905b80c
Changed to recommended typical structure
lazy-dude Jul 10, 2021
e771021
docs: Added some doxygen doc.
lazy-dude Jul 14, 2021
157f6d1
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
bbb6024
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
82e1f41
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
1e4c396
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
166dc0b
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
57bcffa
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
129f4b0
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8526600
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
61bed00
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
4d91c9d
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8defb93
Followed the two suggestions on code. Explain includes, intmax_t->uin…
lazy-dude Jul 17, 2021
6397733
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
45e4eff
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
6553640
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
09df4a4
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
63cf38c
Changes of some types.
lazy-dude Jul 18, 2021
e078df2
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
0b199fb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
f66cd1b
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
fd82d9e
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
ebf83bb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
63a9a16
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
a2ca660
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
81ab47c
Considered some suggestions
lazy-dude Jul 21, 2021
0f96de5
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
254cabf
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8c1b548
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8632490
Added link for explaination.
lazy-dude Jul 22, 2021
8fa92c5
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
4ab5a25
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
da73878
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
71c17ee
return -> returns
lazy-dude Jul 23, 2021
c3433f7
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
53e719b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
af4b346
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
aa8e55b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
0d61a4a
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
82fb3ae
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
9612325
Added description
lazy-dude Jul 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 96 additions & 16 deletions conversions/binary_to_decimal.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,104 @@
/**
* @file
* @brief Converts a [binary number to a decimal](https://byjus.com/binary-to-decimal-formula/) number.
* @details
* A binary number is an input that is checked to be binary
* then, the number is converted to a decimal number.
* @author [Kyler Smith](https://github.com/KylerSmith)
* @author [lazy-dude](https://github.com/lazy-dude)
*/

#include <assert.h> /// for assert
#include <stdbool.h> /// for bool
#include <stdint.h> /// for uintmax_t
#include <stdio.h> /// for IO operations

/**
* Modified 07/12/2017, Kyler Smith
*
* @brief checks whether the number is binary
* @param num the number to be checked if it has binary representation
* @returns `true` if the number IS binary
* @returns `false` if the number is NOT binary
*/
bool is_binary(uintmax_t num)
{
unsigned remainder = 0;

#include <stdio.h>
while (num > 0) {
remainder = num % 10;
// make sure each digit is 0 or 1
if (remainder == 0 || remainder == 1) {
num /= 10;
continue;
} else
return false;
}
return true;
}

int main()
/**
* @brief finds the length of an `uintmax_t` number
* @param num whose length to be computed
* @returns the length of the number
*/
uint16_t num_len(uintmax_t num)
{
int remainder, number = 0, decimal_number = 0, temp = 1;
printf("\n Enter any binary number= ");
scanf("%d", &number);

// Iterate over the number until the end.
while (number > 0)
{
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
uint16_t len = 0;
for (len = 0; num > 0; len++) {
num /= 10;
}
return len;
}

/**
* @brief The `binary_decimal` function does the actual job of conversion
* @param number the number binary to be converted
* @returns decimal_number decimal representation of a binary number
*/
uintmax_t binary_decimal(uintmax_t number)
{
uint8_t remainder;
uintmax_t decimal_number = 0;
uint32_t temp = 1;

printf("%d\n", decimal_number);
uint16_t length = num_len(UINTMAX_MAX) - 1;

assert(num_len(number) <= length);
assert(is_binary(number));

// Iterate over the number until the end.
while (number > 0) {
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
}

return decimal_number;
}

/**
* @brief Self-test implementations
* @returns void
*/
static void test()
{
assert(binary_decimal(0)==0);
assert(binary_decimal(1)==1);
assert(binary_decimal(1110001)==113);
assert(binary_decimal(11111111)==255);
assert(binary_decimal(10000000000)==1024);
assert(binary_decimal(1001110100000100)==40196);

printf("All tests have passed!\n");
}

/**
* @brief main function
* @param void
* @returns 0 on exit
*/
int main()
{
test(); // run self-test implementations
return 0;
}
9 changes: 5 additions & 4 deletions conversions/decimal_to_hexa.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int main()
* number****************/
void decimal2Hexadecimal(long num)
{
char hex_letters[]="abcdef";
long decimalnum = num;
long quotient, remainder;
int i, j = 0;
Expand All @@ -29,17 +30,17 @@ void decimal2Hexadecimal(long num)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
hexadecimalnum[j++] = '0' + remainder;

else
hexadecimalnum[j++] = 55 + remainder;
hexadecimalnum[j++] = hex_letters[remainder-10];// 'A'

quotient = quotient / 16;
}

// print the hexadecimal number

for (i = j; i >= 0; i--)
printf("0x");
for (i = j-1; i >= 0; i--)
{
printf("%c", hexadecimalnum[i]);
}
Expand Down