/*
* =====================================================================================
*
* Filename: binaer_dezimal.c
*
* Description: Binär in Dezimal und andersherum umrechnen.
*
* Version: 1.0
* Created: 28.01.2013 18:16:20
* Revision: none
* Compiler: gcc
*
* Author: H4ckHunt3r
*
* Webseite: www.h4ckhunt3r.net
* ICQ: 58-92-35
* Skype: h4ckhunt3rnet
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define BIN_SIZE 32
/*-----------------------------------------------------------------------------
* TYPE DEFINITIONS
*-----------------------------------------------------------------------------*/
typedef _Bool bin;
typedef unsigned int uint;
/*-----------------------------------------------------------------------------
* FUNCTION PROTOTYPES
*-----------------------------------------------------------------------------*/
void int_to_bin(uint *i, bin b[]);
void bin_to_int(bin b[], uint *i);
void print_bin(bin b[]);
uint power ( uint x, uint n );
/*
* === FUNCTION ======================================================================
* Name: main
* Description: Hauptprogramm
* =====================================================================================
*/
int main ( int argc, char *argv[] )
{
uint exp_int = 327;
bin exp_int_bin[BIN_SIZE] = {0};
bin exp_bin[BIN_SIZE] = {0};
exp_bin[3] = 1;
uint exp_bin_int = 0;
printf("%20s %u\n", "exp_int:", exp_int);
int_to_bin(&exp_int, exp_int_bin);
printf("%20s ", "exp_int -> bin:");
print_bin(exp_int_bin);
printf("%20s ", "exp_bin:");
print_bin(exp_bin);
bin_to_int(exp_bin, &exp_bin_int);
printf("%20s %u\n", "exp_bin -> int:", exp_bin_int);
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
/*
* === FUNCTION ======================================================================
* Name: print_bin
* Description: Binärzahl ausgeben
* =====================================================================================
*/
void print_bin(bin b[])
{
assert(b != NULL);
uint counter;
for(counter = 0; counter < BIN_SIZE; counter++)
{
printf("%d", (int)b[counter]);
if( (counter+1)%4 == 0) printf(" ");
}
printf("\n");
}
/*
* === FUNCTION ======================================================================
* Name: int_to_bin
* Description: Ganzzahl (Dezimal) in Binärzahl umwandeln
* =====================================================================================
*/
void int_to_bin(uint *i, bin b[])
{
assert(i != NULL);
assert(b != NULL);
uint mask = 1U;
uint x = *i;
uint counter;
for(counter = 0; counter < BIN_SIZE; counter++)
{
if( (x & mask) != 0 )
{
b[(BIN_SIZE - counter)-1] = 1;
}
else
{
b[(BIN_SIZE - counter)-1] = 0;
}
mask <<= 1;
}
} /* ---------- end of function int_to_bin ---------- */
/*
* === FUNCTION ======================================================================
* Name: bin_to_int
* Description: Binärzahl zu Ganzzahl (Dezimal) umwandeln
* =====================================================================================
*/
void bin_to_int(bin b[], uint *i)
{
assert(b != NULL);
assert(i != NULL);
uint counter;
uint erg = 0;
for(counter = 0; counter < BIN_SIZE; counter++)
{
erg += b[(BIN_SIZE - counter)-1] * power(2, counter);
}
*i = erg;
}
/*
* === FUNCTION ======================================================================
* Name: power
* Description: Potenzieren von Ganzzahlen
* Parameter: x: Basis (Ganzzahl)
* n: Exponent (Ganzzahl)
* =====================================================================================
*/
uint power ( uint x, uint n )
{
uint erg = 1;
while ( n != 0 )
{
if(n % 2)
{
erg *= x;
}
n /= 2;
x *= x;
}
return erg;
} /* ----- end of function power ----- */