I am writing a program that converts user input to either
ASCII values or binary values. If the string contains letters,
each letter should be converted to ASCII. Strings of numbers will
be converted to binary value of entire string. If both letters and
numbers are entered, each letter will be converted to ASCII and the numbers can/will only be separated by letters, for example "32" will print the binary value "00100000", but "3a2" should be converted to "00000011", "97", "00000010".
The way the program is currently written, strings of numbers convert to binary perfectly. However, strings of letters add a decimal "0" to the end. The output converts each letter to its ASCII value, then converts the "0" to binary. I am unsure as to where this "0" is coming from. Additionally, strings beginning
and ending with digits (for example "6j3") will print the ASCII value of j, then the binary value of "6", skipping the "3" entirely and printing the "j" before the "6". I would like to print each ASCII/binary value in the exact order of the user input.
I am posting my entire code for any necessary clarification, but I believe the issue is in the determineChars() function. I am also looking to use the char* letters and char* numbers functions to efficiently handle the appropriate data and store the final num[] and let[] arrays, but I am unsure of how to do this.
I am quite a beginner to C, so excuse the messiness. Corrections, as well as any further optimizations would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#define EIGHT_BITS 255
#define SIXTEEN_BITS 65535
#define THIRTY_TWO_BITS 4294967295UL
#define SIXTY_FOUR_BITS 18446744073709551615ULL
// defined in case of booleans
typedef enum { false, true } bool;
// GET # OF ELEMENTS IN STRING
size_t getSize(char* input) {
size_t size;
size = strlen(input);
printf("Number of Characters..... %d", size);
//printf("\n----------------------------------");
return size;
}
// DETERMINE NUMBER OF BITS TO OUTPUT
int getBitLength(unsigned long long d) {
int l;
if (d <= EIGHT_BITS) {
l = 8;
}
else if (d > EIGHT_BITS && d <= SIXTEEN_BITS) {
l = 16;
}
else if (d > SIXTEEN_BITS && d <= THIRTY_TWO_BITS) {
l = 32;
}
else if (d > THIRTY_TWO_BITS && d <= SIXTY_FOUR_BITS) {
l = 64;
}
printf("\nBits..................... %d", l);
return l;
}
// CONVERT INPUT TO BINARY VALUE
void convertToBinary(char* input) {
static int b[64];
int i, j, length, r;
unsigned long long decimal;
char* pEnd;
// converts input to ull
decimal = strtoull(input, &pEnd, 0);
printf("\n\n---------------- %I64u ------------------", decimal);
printf("\nULL...................... %I64u", decimal);
length = getBitLength(decimal);
// creates array
for (i = 0; i < length; i++) {
r = decimal % 2;
decimal /= 2;
b[i] = r;
}
// reverses array for binary value
printf("\nBinary Value............. ");
for (j = length - 1; j >= 0; j--) {
printf("%d", b[j]);
}
}
char* numbers(char* input) {
char* num = (char*) malloc(sizeof(char) * 25);
return num;
}
char* letters(char* input) {
char* let = (char*) malloc(sizeof(char) * 25);
return let;
}
void determineChars(char* input) {
int i;
char* num = numbers(input);
char* let = letters(input);
size_t inputSize = getSize(input);
// FOR EACH CHARACTER IN INPUT
for (i = 0; i < inputSize; i++) {
if (isdigit(input[i])) {
// stores number values from input into separate array
num[i] = input[i];
printf("\nNumbers: %c", num[i]);
}
if (!isdigit(input[i])) {
// stores letter values from input into separate array
let[i] = input[i];
printf("\nLetters: %c", let[i]);
// prints separator line + ASCII value
printf("\n\n---------------- %c ------------------", let[i]);
printf("\nASCII Value of %c......... %d", let[i], let[i]);
// removes char from input array
input[i] = ' ';
}
}
// char array must consist of digits only
convertToBinary(num);
}
int main() {
// GET INPUT
char input[50];
scanf("%s", input);
determineChars(input);
return 0;
}
I would like to print each ASCII/binary value in the exact order of the user input.
In that case, you would have to restructure your code a bit. This is because if the input contains only digits you will have to print binary and alternate being chars and digits if the string contains both. I have tried to do this with the following code, cleaned it up a bit, removed the warnings and memory leaks.
See if this is what you want:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#define EIGHT_BITS 255
#define SIXTEEN_BITS 65535
#define THIRTY_TWO_BITS 4294967295UL
#define SIXTY_FOUR_BITS 18446744073709551615ULL
// GET # OF ELEMENTS IN STRING
size_t getSize(char* input) {
size_t size;
size = strlen(input);
printf("Number of Characters..... %d\n", size);
//printf("\n----------------------------------");
return size;
}
// DETERMINE NUMBER OF BITS TO OUTPUT
int getBitLength(unsigned long long d) {
int l;
if (d <= EIGHT_BITS) {
l = 8;
}
else if (d > EIGHT_BITS && d <= SIXTEEN_BITS) {
l = 16;
}
else if (d > SIXTEEN_BITS && d <= THIRTY_TWO_BITS) {
l = 32;
}
else if (d > THIRTY_TWO_BITS && d <= SIXTY_FOUR_BITS) {
l = 64;
}
printf("\nBits..................... %d", l);
return l;
}
// CONVERT INPUT TO BINARY VALUE
void convertToBinary(char* input) {
static int b[64];
int i, j, length, r;
unsigned long long decimal;
char* pEnd;
// converts input to ull
decimal = strtoull(input, &pEnd, 0);
printf("\n---------------- %I64u ------------------", decimal);
printf("\nULL...................... %I64u", decimal);
length = getBitLength(decimal);
// creates array
for (i = 0; i < length; i++) {
r = decimal % 2;
decimal /= 2;
b[i] = r;
}
// reverses array for binary value
printf("\nBinary Value............. ");
for (j = length - 1; j >= 0; j--) {
printf("%d", b[j]);
}
printf("\n");
}
void determineChars(char* input) {
int i;
long ret;
char* ptr;
char c;
size_t inputSize = getSize(input);
ret = strtol(input, &ptr, 10);
if((ret == 0) || ((strlen(ptr) != 0) && (strlen(input) != strlen(ptr))))
{
for (i = 0; i < inputSize; i++) {
if (isdigit(input[i])) {
c = input[i];
printf("\nNumber: %c", c);
convertToBinary(&c);
}
if (!isdigit(input[i])) {
// stores letter values from input into separate array
printf("\nLetter: %c", input[i]);
// prints separator line + ASCII value
printf("\n---------------- %c ------------------\n", input[i]);
printf("ASCII Value of %c......... %d\n", input[i], input[i]);
// removes char from input array
}
}
}
else
convertToBinary(input);
}
int main() {
// GET INPUT
char input[50];
scanf("%s", input);
determineChars(input);
}
I also tried out the test cases you mentioned in the question along with few others and it seems to work fine.
32
Number of Characters..... 2
---------------- 32 ------------------
ULL...................... 32
Bits..................... 8
Binary Value............. 00100000
3a2
Number of Characters..... 3
Number: 3
---------------- 3 ------------------
ULL...................... 3
Bits..................... 8
Binary Value............. 00000011
Letter: a
---------------- a ------------------
ASCII Value of a......... 97
Number: 2
---------------- 2 ------------------
ULL...................... 2
Bits..................... 8
Binary Value............. 00000010
6j3
Number of Characters..... 3
Number: 6
---------------- 6 ------------------
ULL...................... 6
Bits..................... 8
Binary Value............. 00000110
Letter: j
---------------- j ------------------
ASCII Value of j......... 106
Number: 3
---------------- 3 ------------------
ULL...................... 3
Bits..................... 8
Binary Value............. 00000011
Related
Why is it returning 7 digits instead of 8 digits once binary array's length is 8 (meaning 8 bits)?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int number = get_int("Number: ");
char binary[8]; // 8 bits in a byte
for (int i = 0; number != 0 ; i++)
{
if (number % 2 == 0)
{
binary[i] = '0';
}
else
{
binary[i] = '1';
}
number /= 2;
}
printf("%s\n", binary);
}
I'm getting
Number: 72
Binary: 0001001
I know it's reversed from the correct answer for this decimal, just want to correct the missing digit first.
For your program, I only had to make one small correction:
https://ideone.com/87v18W
int main(void)
{
int number = 145;
char binary[8+1] = {}; // 8 bits in a byte, + 1 for string-terminator
// If you're going to print it out as a string, you have to make sure there is space for a nil-terminator ('\0') at the end of the string.
// Hence the +1 for space, and the ={}; to set the whole thing to 0 before filling in the bits.
for (int i = 0; number != 0 ; i++)
{
if (number % 2 == 0)
{
binary[i] = '0';
}
else
{
binary[i] = '1';
}
number /= 2;
}
printf("%s\n", binary);
}
When I run this program, with input of 145, I get
Output
10001001
Here's how I do ints to binary, with a . separator every 8 bits:
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
int main(void) {
uint32_t num = 93935;
for(int i=(sizeof(uint32_t)*CHAR_BIT)-1; i>=0; --i)
{
printf("%c%s", "01"[!!(num & (1<<i))], i%8?"":i?".":"");
}
return 0;
}
Example Output:
Success #stdin #stdout 0s 5404KB
00000000.00000001.01101110.11101111
I have an assignment that is asking me to convert a decimal number to its 16-bit 2's complement binary representation stored in a char array. I was provided started code that included a complete main method and an addOne method. I just needed to complete the flipBits and magnitudeToBinary methods.
I have completed the methods and they should work, but I am getting string output. An example is:
Enter integers and convert it to 2's complement binary.
Non-numeric input terminates program.
27
Integer: 27, 2's complement binary:
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void magnitudeToBinary(int n, char arr[], int numOfBits);
void flipBits(char arr[], int numOfBits);
void addOne(char arr[], int numOfBits);
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
//Declare a char array with size 16 to simulate 16-bit binary
int numOfBits = 16;
char arr[numOfBits + 1];
arr[numOfBits] = '\0'; //set the terminating character
//Declare integer n to hold the decimal integer
int n = 0;
puts("Enter integers and convert it to 2's complement binary.");
puts("Non-numeric input terminates program.");
//Continually taking input and convert it to binary
while (scanf("%d", &n) == 1) {//if successfully read in ONE decimal integer
//Initialize the char array to all 0s (leave the terminating character unchanged)
for(int i = 0; i < numOfBits; i ++){
arr[i] = '0';
}
//Convert magnitude (absolute value) to binary
magnitudeToBinary(abs(n), arr, numOfBits);
//if the number is negative: flip all bits, then add 1.
if(n < 0){
//Flip all bits in char arr
flipBits(arr, numOfBits);
//Add 1
addOne(arr, numOfBits);
}
//Output binary:
printf("Integer: %d, 2's complement binary: %s\n", n, arr);
}
return EXIT_SUCCESS;
}
/* addOne: arithmatically add 1 to the binary simulated by character array
* INPUT: char arr[]: the character array holding the binary
* int numOfBits: the number of bits in the binary
* */
void addOne(char arr[], int numOfBits){
/* True table
* ******************************
* carry arr[i] | arr[i] carry
* 1 0 | 1 0
* 1 1 | 0 1
* 0 0 | 0 0
* 0 1 | 1 0
* *********************************/
char carry = '1';
for(int i = numOfBits - 1; i >= 0; i --){
if(carry != arr[i]){
arr[i] = '1';
carry = '0';
}
else if(carry == '1'){
arr[i] = '0';
}
else{
arr[i] = '0';
}
}
return;
}
/* flipBits: perform 1's complement on the binary, i.e., change 1 to 0, 0 to 1
* INPUT: char arr[]: the character array holding the binary
* int numOfBits: the number of bits in the binary *
* */
void flipBits(char arr[], int numOfBits){
//Implement your solution here
for(int i = 0; i < numOfBits; i++) {
if(arr[i] == '1') {
arr[i] = '0';
} else if(arr[i] == '0') {
arr[i] = '1';
}
}
return;
}
/* magnitudeToBinary: Convert a non-negative decimal integer to binary (stored in a char array)
* using division-remainder algorithm
* INPUT: int n: The decimal integer number to be converted
* char arr[]: the character array holding the binary
* int numOfBits: the number of bits in the binary *
* */
void magnitudeToBinary(int n, char arr[], int numOfBits){
//Implement the division-remainder algorithm here
int i = 0;
while (n > 0) {
arr[i] = n % 2;
n = n / 2;
i++;
}
return;
}
I would just like to see the correct binary output. Right now I am unsure if it is an error on my part, my professor's, or if Eclipse is acting up. Any help is appreciated, thanks!
Try changing the line in magnitudeToBinary() from :
arr[i] = n % 2;
to
arr[i] = n % 2 == 1 ? '1' : '0';
You want to write the ASCII value for '1' or '0' into the array - not the result of n % 2.
I pass in a hex number into hex2bin and it prints out the binary number correctly but I don't want it to print out the number I want to return the number so I can use it to find the cardinality of the number. How would I store the number instead of printing it out?
int hex2bin (int n){
int i,k,mask;
for(i = sizeof(int) * 8 - 1; i >= 0; i--){
mask = 1 << i;
k = n & mask;
k == 0 ? printf("0"):printf("1");
}
return 0;
}
Perhaps something like this?
int result = 0;
int i, k...
...
result = result | (((k == 0) ? 0 : 1) << i;
...
return result;
Instead of being clever with an int, you could of course also simply use an array of variables instead.
Store the number in a string whose space is provided by a compound literal (Available since C99).
It works like OP's flow: Loop up to sizeof(int) * 8 times, finding the value of 1 bit and print/save it.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Maximum buffer size needed
#define UTOA_BASE_2 (sizeof(unsigned)*CHAR_BIT + 1)
char *utoa_base2(char *s, unsigned x) {
s += UTOA_BASE_2 - 1;
*s = '\0';
do {
*(--s) = "01"[x % 2];
x /= 2;
} while (x);
return s;
}
#define TO_BASE2(x) utoa_base2((char [UTOA_BASE_2]){0} , (x))
void test(unsigned x) {
printf("base10:%10u base2:%5s ", x, TO_BASE2(x));
char *s = TO_BASE2(x);
// do stuff with `s`, it is valid for until the end of this block
printf("%s\n", s);
}
int main(void) {
test(0);
test(25);
test(UINT_MAX);
}
Sample output
base10: 0 base2: 0 0
base10: 25 base2:11001 11001
base10:4294967295 base2:11111111111111111111111111111111 11111111111111111111111111111111
This is a variation of this base-n answer.
You can use the strcat function to do that.
Note that the new hex2bin function in this answer assumes that the parameter char *buf has already been allocated and can hold at least 1+sizeof(int)*8 bytes including the null terminator:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// assume: buf is at least length 33
int hex2bin (int n, char *buf)
{
int i,k,mask;
for(i = sizeof(int) * 8 - 1; i >= 0; i--){
mask = 1 << i;
k = n & mask;
k == 0 ? strcat(buf, "0") : strcat(buf, "1");
}
return 0;
}
int main()
{
int n = 66555;
char buffer[1+sizeof(int)*8] = { 0 } ;
hex2bin(n, buffer);
printf("%s\n", buffer);
return 0;
}
I hope you will find this helpful :)
bool convertDecimalBNR(INT32 nDecimalValue, UINT32 * punFieldValue, INT32 nBitCount, DecimalBNRType * pDecimalSpecification)
{
bool bBNRConverted = false;
INT32 nBitIndex = nBitCount - 1;
INT32 nBitValue = anTwoExponents[nBitIndex];
*punFieldValue = 0;
if ((nDecimalValue >= pDecimalSpecification->nMinValue) && (nDecimalValue <= pDecimalSpecification->nMaxValue))
{
// if the value is negative, then add (-1 * (2 ^ (nBitCount - 1))) on itself and go on just like a positive value calculation.
if (nDecimalValue < 0)
{
nDecimalValue += nBitValue;
nBitIndex--;
nBitValue /= 2;
*punFieldValue |= BIT_0_ONLY_ONE;
}
while (nBitIndex >= 0)
{
*punFieldValue = (*punFieldValue << 1);
if (nDecimalValue >= nBitValue)
{
nDecimalValue -= nBitValue;
*punFieldValue |= BIT_0_ONLY_ONE;
}
nBitIndex--;
nBitValue /= 2;
}
if (nDecimalValue <= nBitValue)
{
bBNRConverted = true;
}
}
return (bBNRConverted);
}
I cannot seem to get my output to work correctly. I want to make it so that the last remainder in parenthesis is lined up perfectly in a column following the first line's spacing. I have most of the output right except for this last part and it has to be exactly the way my professor wants it.
The best I've been able to come up with is EDITED:
Marcus Lorenzana
314156 = 19634 * 16 + 12 (C)
19634 = 1227 * 16 + 2 (2)
1227 = 76 * 16 + 11 (B)
76 = 4 * 16 + 12 (C)
4 = 0 * 16 + 4 (4)
0x4CB2C
This is the output I want:
Marcus Lorenzana
314156 = 19634 * 16 + 12 (C)
19634 = 1227 * 16 + 2 (2)
1227 = 76 * 16 + 11 (B)
76 = 4 * 16 + 12 (C)
4 = 0 * 16 + 4 (4)
0x4CB2C
But as you can see it the output is not exactly correct.
Here is my program EDITED:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 50
static int base = 16;
int main(int argc, char * argv[]) {
printf("Marcus Lorenzana\n");
if (argc == 2) {
char hexstr[] = "0123456789ABCDEF";
int i = 0;
long long oldresult;
int remainder;
char remainders[BUFFER];
char w_num[BUFFER];
long long value = atoll(argv[1]);
//Get width of original number for formatting purposes
int vwidth = strlen(argv[1]);
char oldwidth[BUFFER];
//Convert the decimal to hexadecimal
while(value != 0) {
oldresult=value;
remainder = value%base;
value = value/base;
//Store the remainder in an array for later use
remainders[i]=hexstr[remainder];
char line[BUFFER];
//Get length of line for formatting purposes
int w = sprintf(line,"%*lld = %-*lld * %2d + %2d", \
vwidth,oldresult,vwidth,value,base,remainder);
printf("%s (%c)\n", line,hexstr[remainder]);
i++;
}
//Print the the hexadecimal number
int x = i;
printf("0x");
while(x > 0) {
printf("%c",remainders[--x]);
}
printf("\n");
} else {
printf("Error: Wrong arguments\n");
return 1;
}
return 0;
}
Modify based on your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 50
static int base = 16;
int main(int argc, char * argv[]) {
printf("Marcus Lorenzana\n");
if (argc == 2) {
char hexstr[] = "0123456789ABCDEF";
int i = 0;
long long oldresult;
int remainder;
char remainders[BUFFER];
char w_num[BUFFER];
long long value = atoll(argv[1]);
//Get width of original number for formatting purposes
int vwidth = strlen(argv[1]);
char oldwidth[BUFFER];
int wMax = 0;
//Convert the decimal to hexadecimal
while(value != 0) {
oldresult=value;
remainder = value%base;
value = value/base;
//Store the remainder in an array for later use
remainders[i]=hexstr[remainder];
char line[BUFFER];
//Get length of line for formatting purposes
int w = sprintf(line,"%*lld = %-lld * %2d + %-2d", \
vwidth,oldresult,value,base,remainder);
wMax = w > wMax ? w:wMax;
printf("%s %*s(%c)\n", line,wMax-w,"",hexstr[remainder]);
i++;
}
//Print the the hexadecimal number
int x = i;
printf("0x");
while(x > 0) {
printf("%c",remainders[--x]);
}
printf("\n");
} else {
printf("Error: Wrong arguments\n");
return 1;
}
return 0;
}
Your idea to print the right-hand side to a temporary string is good, but for the output you want, you should just print the stuff to thr right of the equals sign to that string. Also, because you don't want the operands lined up, strip all formatting information, i.e. the widths, from the string.
snprintf(line, BUFFER, "%lld * %d + %d", value, base, remainder);
printf("%*lld = %*s (%c)\n", vwidth, oldresult,
-(vwidth + 10), line, hexstr[remainder]);
The width of the rhs is calculated based on the initial length of the number, vwidth plus two times two for the numbers plus two times three for the operands with surrounding spaces. The width has to be negative, because you want the RHS to be left-aligned and passed with spaces to the right.
If you let printf do the padding, there's no need to store the string length from the sprintf call, w.
Let us say we have the following number.
Number = 2001000200030
How can I grab the first digit and store it to a variable? Then
grab the next four digits and store them to another variable?
Then the next four digits ...
So the output should be like this.
first = 2;
firstfour = 0010
secondfour = 0020
thirdfour = 0030
Thank you and I appreciate your time.
Why use strings when you can stick with processing numbers? You can split by digits using modulo and division in powers of 10.
#include <stdio.h>
int main(void) {
long long number = 2001000200030LL;
/* Find power of 10 for top digit */
long long currentDigit = 1LL;
while((currentDigit * 10LL) < number) currentDigit *= 10LL;
printf("First digit = %lld\n", number / currentDigit);
number %= currentDigit;
/* Read off groups of four digits */
while(currentDigit >= 10000LL)
{
long long nextFour;
currentDigit /= 10000LL;
nextFour = number / currentDigit;
number %= currentDigit;
printf("Next four = %04lld\n", nextFour);
}
/* Output any remaining digits not covered by a group of four */
if (currentDigit > 1LL)
{
printf("Remaining digits = ");
for(currentDigit /= 10LL; currentDigit > 1LL; currentDigit /= 10LL)
{
printf("%lld", (number / currentDigit) % 10LL);
}
printf("%lld\n", number % 10LL);
}
return 0;
}
Numbers are stored on the computer in binary, because of this an integer has no capacity to distinguish individual digits.
Convert the number to a string and extract the parts you need. You can also convert each part back into a number if needed. (There are probably prettier ways of extracting parts of the string)
Also this code is for this contrived example, if the integer is unknown, this code cannot safely make the assumptions it does.
long long number = 2001000200030LL;
// convert number to string
char string[64];
sprintf(string, "%lld", number);
// access individual digits
string[0] (first digit)
string[1] (second digit)
// copy first four digits into an int
char firstFour[5]
memcpy(firstFour, string, 4)
firstFour[4] = "\0"; // null terminate
int firstFourInt = atoi(firstFour);
// copy second four digits into an int
char secondFour[5]
memcpy(secondFour, string + 4, 4)
secondFour[4] = "\0"; // null terminate
int secondFourInt = atoi(secondFour);
#include <stdio.h>
#include <string.h>
#include <stdint.h>
int main(void){
int64_t number = 2001000200030LL;
char data[24];
char digit[5] ={0}, *p;
int len, r;
len=sprintf(data, "%lld", number);
p = data;
r = len % 4;
if(r){
strncpy(digit, p, r);
printf("%s\n", digit);
len -= r;
p += r;
}
while(len!=0){
strncpy(digit, p, 4);
printf("%s\n", digit);
len -= 4;
p += 4;
}
return 0;
}
/* output
2
0010
0020
0030
*/
case of the number was a string, and output to string.
#include <stdio.h>
#include <string.h>
int main(void){
char number[] = "2005001000200";
char mode[2]={0};
char red[5]={0};
char green[5]={0};
char blue[5]={0};
strncpy(mode , &number[0], 1);
strncpy(red , &number[1], 4);
strncpy(green, &number[5], 4);
strncpy(blue , &number[9], 4);
printf("Mode = %s\n" , mode);
printf("Red = %s\n" , red);
printf("Green = %s\n", green);
printf("Blue = %s\n" , blue);
return 0;
}