Skip to content

I need a Programmer

Project details

Your task is to develop a C program, called ascii2bin, that

reads a string of 1’s and 0’s as ASCII digits, and
outputs the equivalent decimal number
Your program must

exercises the read() system call to read a single byte, at a time, from stdin
validate that the read byte is appropriate for conversion, e.g., it must be either an ASCII ‘0’ or ‘1’
converts each byte into an integer value via a mathematical expression
uses the resulting integer as part of the calcuation to determine the final number
identifies the end of a input string by either end of file or by a new line
End of file is detected when read() returns the value ‘0’
A new line is identified in the ASCII table as either: newline, nl, LF, or n’
prints this final number on stdout
returns a value of 0 upon success and 1 otherwise
The Algorithm
offset = ?;
number = 0;

retval = read(0, &ascii_value, 1);
while (retval == 1)
digit = ascii_value – offset;
number = number << 1 + digit;
retval = read(0, &ascii_value, 1);

printf(“%dn”, number);
return 0;
Validation Checks:
You should add additional validation checks to your code to catch potential errors. At a minimum, validate the following:

that each ASCII input character is one of the following characters: ‘0’, ‘1’, or ‘n’
that the calculated number does not exceed 2^32

I am having trouble with this program I have most of the code but my output does not match the Professors completely:
#include <stdio.h>
#include <unistd.h>

int main()
{
int offset = 48; // ascii value of ‘0’
int number = 0;
char ascii_value;

int retval = read(0, &ascii_value, 1);
while ((retval == 1) && (ascii_value != ‘n’)) {
int digit = ascii_value – offset;
if ((digit == 1) || (digit == 0))
number = (number << 1) + digit;
retval = read(0, &ascii_value, 1);
}

printf(“%un”, number);

return 0;
}

These are his test file information:

$ script
$ ascii2bin
0101
^d
5
$ cat | ./ascii2bin ; echo $?
54356
0
$ cat | ./ascii2bin ; echo $?
138
0
$ cat | ./ascii2bin ; echo $?
2863311530
0
$ cat | ./ascii2bin ; echo $?
4294967295
0
$ cat | ./ascii2bin ; echo $?
1

Awarded to:

Project budget: $10 USD
budget limits: $10-30 USD
number of bids: 9
average bids: $22

Do you have a similar project. Contact us now to help you get it done

Email
Project Type*

Awarded to:

Project budget: $10 USD
budget limits: $10-30 USD
number of bids: 9
average bids: $22

Project details

Your task is to develop a C program, called ascii2bin, that

reads a string of 1’s and 0’s as ASCII digits, and
outputs the equivalent decimal number
Your program must

exercises the read() system call to read a single byte, at a time, from stdin
validate that the read byte is appropriate for conversion, e.g., it must be either an ASCII ‘0’ or ‘1’
converts each byte into an integer value via a mathematical expression
uses the resulting integer as part of the calcuation to determine the final number
identifies the end of a input string by either end of file or by a new line
End of file is detected when read() returns the value ‘0’
A new line is identified in the ASCII table as either: newline, nl, LF, or n’
prints this final number on stdout
returns a value of 0 upon success and 1 otherwise
The Algorithm
offset = ?;
number = 0;

retval = read(0, &ascii_value, 1);
while (retval == 1)
digit = ascii_value – offset;
number = number << 1 + digit;
retval = read(0, &ascii_value, 1);

printf(“%dn”, number);
return 0;
Validation Checks:
You should add additional validation checks to your code to catch potential errors. At a minimum, validate the following:

that each ASCII input character is one of the following characters: ‘0’, ‘1’, or ‘n’
that the calculated number does not exceed 2^32


I am having trouble with this program I have most of the code but my output does not match the Professors completely:
#include <stdio.h>
#include <unistd.h>

int main()
{
int offset = 48; // ascii value of ‘0’
int number = 0;
char ascii_value;

int retval = read(0, &ascii_value, 1);
while ((retval == 1) && (ascii_value != ‘n’)) {
int digit = ascii_value – offset;
if ((digit == 1) || (digit == 0))
number = (number << 1) + digit;
retval = read(0, &ascii_value, 1);
}

printf(“%un”, number);


return 0;
}





These are his test file information:

$ script
$ ascii2bin
0101
^d
5
$ cat | ./ascii2bin ; echo $?
54356
0
$ cat | ./ascii2bin ; echo $?
138
0
$ cat | ./ascii2bin ; echo $?
2863311530
0
$ cat | ./ascii2bin ; echo $?
4294967295
0
$ cat | ./ascii2bin ; echo $?
1

Skills: C# Programming

Do you have a similar project. Contact us now to help you get it done

Email
Project Type*

Other freelancers

Responsive image

Augurs Technologies

Leading Tech Solutions Provider at Freelancer

4.9 (353 reviews)

Responsive image

Ismet A.

.NET/Blockchain Senior Developer & Project Manager

3.1 (25 reviews)

Responsive image

Borko R.

Senior full stack developer.

4.8 (17 reviews)

Responsive image

Mohammad Suman K.

C# | MSSQL | VB | MySQL | .NET Core | MS ACCESS

5.0 (584 reviews)

×

Hello!

Do you have an idea or project. Contact us and we'll get it done for you

× Do you have an idea or project?