Reputation: 11
I wrote a function that prompts the user to input binary number sequence of 0-s and 1-s and outputs the following.
A) Decimal representation of binary number
B) Hexadecimal representation of binary number (base 16)
If the input is invalid (that is, if bin_num contains digits different from 0 and 1), the user should receive an error message (detailed below).
Example of the program run:
Please enter binary number input: 112011 invalid input, please try again. Please enter binary number input: 110011 110011 to decimal is: 51 110011 to hexadecimal is: 0x33
In order to solve the problem correctly, I will not be able to read the binary numbers as integers, since it might cause an overflow. On the other hand, the answer (in decimal) will always fit the standard integer size.
My question is How to check the correctness if the binary number(user's input) contains digits different from 0 and 1?
I tried somethin but it doesn't work.
This is my code:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ENTERING_QUESTION "Please choose a question by entering 1-5 (enter 0 to exit):"
#define MAX_SIZE_INPUT 31
#define QUESTION1_INPUT_MESSAGE "Please enter binary number input:"
#define QUESTION1_OUTPUT_MESSAGE_DECIMAL "to decimal is:"
#define QUESTION1_OUTPUT_MESSAGE_HEXADECIMAL "to hexadecimal is:"
#define QUESTION1_ERROR_MESSAGE "invalid input, please try again."
void bin2hexanddec(char *bin_str)
{
for (int i = 0; i < MAX_SIZE_INPUT; i++) {
if (bin_str[i] != '0' && bin_str[i] != '1') {
printf(QUESTION1_ERROR_MESSAGE"\n");
return;
}
}
char *ptr;
long long input_num= strtol(bin_str, &ptr, 10);
int dec = 0, i = 0, rem;
while (input_num != 0) {
rem = input_num % 10;
input_num /= 10;
dec += rem * pow(2, i);
++i;
}
printf("%lld " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %d\n", input_num, dec);
printf("%lld " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %X\n", input_num, dec);
return;
}
.
.
.
int main()
{
char bin_str[MAX_SIZE_INPUT
.
.
.
printf(ENTERING_QUESTION"\n");
scanf("%d", &choice);
if (choice == 1) {
printf(QUESTION1_INPUT_MESSAGE"\n");
scanf("%s", bin_str);
bin2hexanddec(bin_str);
}
.
.
.
return 0;
}
Upvotes: 1
Views: 72
Reputation: 223972
You're checking too many characters:
for (int i = 0; i < MAX_SIZE_INPUT; i++) {
If the input string is less than MAX_SIZE_INPUT
, you're reading past the end of the string that was written. You instead want:
for (int i = 0; i < strlen(bin_str); i++) {
Also, to convert the number, instead of reading it as base 10 and doing translation on it, just tell strtol
to use base 2. You can also check if ptr
points to the null byte at the end of the string to check for invalid characters.
void bin2hexanddec(char *bin_str)
{
char *ptr;
int input_num= strtol(bin_str, &ptr, 2);
if (!*ptr) {
printf(QUESTION1_ERROR_MESSAGE"\n");
return;
}
printf("%s " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %d\n", bin_str, input_num);
printf("%s " QUESTION1_OUTPUT_MESSAGE_DECIMAL " %X\n", bin_str, input_num);
}
Upvotes: 1
Reputation: 34585
You can use the library function strspn
to check the string has only binary digits, as this example shows:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[100];
if(fgets(str, sizeof str, stdin) == NULL) {
// handle error
return 1;
}
str[ strcspn(str, "\n") ] = 0; // remove trailing newline
if (strspn(str, "01") == strlen(str)) {
puts("Binary digits");
}
else {
puts("Not binary");
}
return 0;
}
Upvotes: 2