R
rayw
I've been trying to write a program that converts an entered number into its
binary representation using three different techniques - bit shifting, mod
2, and itoa..
However, I'm getting 'snow blind', and can't get the non-ISO itoa to work as
I'd like. The snow blindness makes me think it's me, and not the function!
The idea of the program was to be able to change #define I_TYPE char to any
scalar type, re-compile, and then play. Limited success so far though, and
I'm sure the code could be 'better'.
Any constructive comments welcome.
#include <stdio.h >
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define I_TYPE char
#define K sizeof(I_TYPE) * CHAR_BIT
// Forward decs.
//
char * binary1(I_TYPE n);
char * binary2(I_TYPE n);
char * binary3(I_TYPE n);
char * strrev (char * str);
int main(void)
{
char buffer[100];
puts("Enter numbers to convert to binary, or hit enter to quit");
while(strlen(fgets(&buffer[0], sizeof(buffer), stdin)) > 1)
{
I_TYPE l = (I_TYPE)strtol(&buffer[0], NULL, 10);
puts(binary1(l));
puts(binary2(l));
puts(binary3(l));
}
return 0;
}
char * binary1(I_TYPE n)
{
// Loop counter.
//
int i = 0;
// Bit-mask.
//
unsigned long int j = 1;
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
for(i = 0; i < K; i++)
{
buffer = (n & j) == j ? '1' : '0';
j = j << 1;
}
return strrev(buffer);
}
char * binary2(I_TYPE n)
{
int i;
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
for(i = K - 1; i >= 0; n /= (unsigned long int)2)
{
buffer[i--] = n % 2 == 0 ? '0' : '1';
}
return buffer;
}
char * binary3(I_TYPE n)
{
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
// itoa() not a standard ISO function.
//
itoa(n, buffer, 2);
return buffer;
}
char * strrev(char * s)
{
char * p1;
char * p2;
if (!s || !*s)
{
return s;
}
else
{
for(p1 = s, p2 = s + strlen(s) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
}
return s;
}
binary representation using three different techniques - bit shifting, mod
2, and itoa..
However, I'm getting 'snow blind', and can't get the non-ISO itoa to work as
I'd like. The snow blindness makes me think it's me, and not the function!
The idea of the program was to be able to change #define I_TYPE char to any
scalar type, re-compile, and then play. Limited success so far though, and
I'm sure the code could be 'better'.
Any constructive comments welcome.
#include <stdio.h >
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define I_TYPE char
#define K sizeof(I_TYPE) * CHAR_BIT
// Forward decs.
//
char * binary1(I_TYPE n);
char * binary2(I_TYPE n);
char * binary3(I_TYPE n);
char * strrev (char * str);
int main(void)
{
char buffer[100];
puts("Enter numbers to convert to binary, or hit enter to quit");
while(strlen(fgets(&buffer[0], sizeof(buffer), stdin)) > 1)
{
I_TYPE l = (I_TYPE)strtol(&buffer[0], NULL, 10);
puts(binary1(l));
puts(binary2(l));
puts(binary3(l));
}
return 0;
}
char * binary1(I_TYPE n)
{
// Loop counter.
//
int i = 0;
// Bit-mask.
//
unsigned long int j = 1;
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
for(i = 0; i < K; i++)
{
buffer = (n & j) == j ? '1' : '0';
j = j << 1;
}
return strrev(buffer);
}
char * binary2(I_TYPE n)
{
int i;
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
for(i = K - 1; i >= 0; n /= (unsigned long int)2)
{
buffer[i--] = n % 2 == 0 ? '0' : '1';
}
return buffer;
}
char * binary3(I_TYPE n)
{
// 'static' essential - buffer is a local variable!!
//
static char buffer[K + 1];
buffer[sizeof(buffer) - 1] = '\0';
// itoa() not a standard ISO function.
//
itoa(n, buffer, 2);
return buffer;
}
char * strrev(char * s)
{
char * p1;
char * p2;
if (!s || !*s)
{
return s;
}
else
{
for(p1 = s, p2 = s + strlen(s) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
}
return s;
}