Convert ASCII String to 7-bit GSM coding scheme - c

A simple routine i wrote for converting ASCII string to corresponding 7-bit GSM coding scheme:
#include <stdio.h>
#include <process.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
/* convert ascii input string to 7-bit GSM alphabet */
bool ascii_to_gsm(const char* in, uint8_t len, uint8_t* out, uint8_t start_indx) {
if (in == NULL || out == NULL || len == 0)
return false;
uint8_t nshift = 7;
memcpy(out + start_indx, in, len);
for (size_t i = 0; i < len - 1; i++) {
nshift = (nshift == 255) ? 7 : nshift;
uint16_t l = out[start_indx + i];
uint16_t h = out[start_indx + i + 1];
h = (h << nshift--) | l;
out[start_indx + i] = h;
out[start_indx + i + 1] = h >> 8;
}
return true;
}
int main() {
char data[] = "ASCIIASCII";
uint8_t buff[sizeof(data) - 1];
memset(buff, 0, sizeof(buff));
ascii_to_gsm(data, sizeof(buff), buff, 0);
for (size_t i = 0; i < sizeof(buff); i++) {
printf("\n buff[%d]=%02x", i, buff[i]);
}
system("pause");
return 0;
}
For strings like ASCII or TEST it's working fine and output is C1E9309904 and D4E2940Arespectively.
But for string ASCIIASCII some output byte is wrong:
C1E930990C4E87498024
The result should be: C1E930990C4E87C924
Don't know what part, i'm wrong.
Concepts about GSM coding can be found here.
I use this online encoder to compare results

But for string ASCIIASCII some output byte is wrong:
C1E930990C4E87498024
The result should be:
C1E930990C4E87C924
OP's code does not take into account the output may have a shorter length than the input.
If the input is 10 ASCII characters, that is 70 bits. The output needs to be ceiling(70/8) or 9 bytes. Also see #Steve Summit.
A simplified code for reference that lacks a start_indx. Since input is a string ("converting ASCII string"), the input length is not needed.
bool ascii_to_gsmA(const char* in, uint8_t* out) {
unsigned bit_count = 0;
unsigned bit_queue = 0;
while (*in) {
bit_queue |= (*in & 0x7Fu) << bit_count;
bit_count += 7;
if (bit_count >= 8) {
*out++ = (uint8_t) bit_queue;
bit_count -= 8;
bit_queue >>= 8;
}
in++;
}
if (bit_count > 0) {
*out++ = (uint8_t) bit_queue;
}
return true;
}

Related

How can I take characters of a string with the data type of uint8_t Variable and use them as hexadecimal variables?

i have an uint8_t Variable which contains a substring of 4 hexadecimal variables. Example:
uint8_t String[10] = "00AABBCC";
I would like to take these 4 hex Variables into different hex values:
uint8_t Data_Byte[4];
Data_Byte[0]=0x00;
Data_Byte[1]=0xAA;
Data_Byte[2]=0xBB;
Data_Byte[3]=0xCC;
How can I take these 4 substrings into 4 different uint8_t Variables?
You can use sscanf to parse each two-character pair in the string into a number:
uint8_t arr[strlen(String) / 2];
for (int i = 0; i < strlen(String); i += 2) {
sscanf(String + i, "%2hhx", &arr[i / 2]);
}
If you're developing on a system with limited sscanf support, you can use something like this:
for (int i = 0; i < strlen(String); i += 2) {
uint8_t val1 = isdigit(String[i]) ? (String[i] - '0') : (String[i] - 'A' + 10);
uint8_t val2 = isdigit(String[i + 1]) ? (String[i + 1] - '0') : (String[i + 1] - 'A' + 10);
arr[i / 2] = val1 << 4 | val2;
}
With your stipulation the strings will represent 4 bytes, this a far-easier-to-read-and-understand solution IMO. I have no comment on efficiency.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <arpa/inet.h>
bool convert(const uint8_t* strValue, uint8_t* cvrtArray)
{
// make 2nd parameter non-NULL for better error checking
errno = 0;
char* endptr = NULL;
// convert to unsigned long
unsigned long val = strtoul((const char*)strValue, &endptr, 16);
// do some error checking, this probably needs some improvements
if (errno == ERANGE && val == ULONG_MAX)
{
fprintf(stderr, "Overflow\n");
return false;
}
else if ((strValue != NULL) && (*endptr != '\0'))
{
fprintf(stderr, "Cannot convert\n");
return false;
}
// potential need to flip the bytes (your string is big endian, and the
// test machine on godbolt is little endian)
val = htonl(val);
// copy to our array
memcpy(cvrtArray, &val, 4);
return true;
}
int main(void)
{
uint8_t Data_Byte[4] = { 0 };
uint8_t String[10] = "00AABBCC";
if (convert(String, Data_Byte) == true)
{
for(size_t i=0; i<sizeof Data_Byte; i++)
{
printf("Data_Byte[%zu] = 0x%02" PRIX8 "\n", i, Data_Byte[i]);
}
}
else
{
fprintf(stderr, "There was a problem converting %s to byte array\n", String);
}
return 0;
}
code in action
I took some inspiration from 0___________ and made my own:
static char digits[] = "0123456789ABCDEF";
void convert(uint8_t *chrs, uint8_t *buff)
{
size_t len = strlen((char *)chrs);
size_t i;
for(i = 0; i < len; i+=2) {
buff[i / 2] = (strchr(digits, chrs[i]) - digits);
buff[i / 2] += (strchr(digits, chrs[i+1]) - digits) << 4;
}
if(i<len)
buff[i / 2] = (strchr(digits, chrs[i]) - digits);
}
The changes are that I find it much more natural to do a complete element in every iteration. To account for odd length input strings, I just added an if statement in the end that takes care of it. This can be removed if input strings always have even length. And I skipped returning the buffer for simplicity. However, as 0___________ pointed out in comments, there are good reasons to return a pointer to the output buffer. Read about those reasons here: c++ memcpy return value
static char digits[] = "0123456789ABCDEF";
uint8_t *convert(uint8_t *chrs, uint8_t *buff)
{
size_t len = strlen((char *)chrs);
for(size_t i = 0; i < len; i++)
{
int is_first_digit = !(i & 1);
int shift = is_first_digit << 2;
buff[i / 2] += (strchr(digits, chrs[i]) - digits) << shift;
}
return buff;
}
int main(void)
{
uint8_t String[] = "00AABBCC";
uint8_t buff[4];
convert(String, buff);
for(size_t i = 0; i < sizeof(buff); i++)
{
printf("%hhx", buff[i]); // I know it is wrong format
}
}
https://godbolt.org/z/9c8aexTvq
Or even faster solution:
int getDigit(uint8_t ch)
{
switch(ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return ch - '0';
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return ch - 'A' + 10;
}
return 0;
}
uint8_t *convert(uint8_t *chrs, uint8_t *buff)
{
size_t len = strlen((char *)chrs);
for(size_t i = 0; i < len; i++)
{
int is_first_digit = !(i & 1);
int shift = is_first_digit << 2;
buff[i / 2] += (getDigit(chrs[i])) << shift;
}
return buff;
}
Remember: use functions for this kind of tasks. Do not program in main.

Converting int to binary String in C

I'm trying to convert an integer to a binary String (see code below). I've already looked at several similar code snippets, and can't seem to find the reason as to why this does not work. It not only doesn't produce the correct output, but no output at all. Can somebody please explain to me in detail what I'm doing wrong?
#include <stdio.h>
#include <stdlib.h>
char* toBinaryString(int n) {
char *string = malloc(sizeof(int) * 8 + 1);
if (!string) {
return NULL;
}
for (int i = 31; i >= 0; i--) {
string[i] = n & 1;
n >> 1;
}
return string;
}
int main() {
char* string = toBinaryString(4);
printf("%s", string);
free(string);
return 0;
}
The line
string[i] = n & 1;
is assigning integers 0 or 1 to string[i]. They are typically different from the characters '0' and '1'. You should add '0' to convert the integers to characters.
Also, as #EugeneSh. pointed out, the line
n >> 1;
has no effect. It should be
n >>= 1;
to update the n's value.
Also, as #JohnnyMopp pointed out, you should terminate the string by adding a null-character.
One more point it that you should check if malloc() succeeded. (It is done in the function toBinaryString, but there is no check in main() before printing its result)
Finally, It doesn't looks so good to use a magic number 31 for the initialization of for loop while using sizeof(int) for the size for malloc().
Fixed code:
#include <stdio.h>
#include <stdlib.h>
char* toBinaryString(int n) {
int num_bits = sizeof(int) * 8;
char *string = malloc(num_bits + 1);
if (!string) {
return NULL;
}
for (int i = num_bits - 1; i >= 0; i--) {
string[i] = (n & 1) + '0';
n >>= 1;
}
string[num_bits] = '\0';
return string;
}
int main() {
char* string = toBinaryString(4);
if (string) {
printf("%s", string);
free(string);
} else {
fputs("toBinaryString() failed\n", stderr);
}
return 0;
}
The values you are putting into the string are either a binary zero or a binary one, when what you want is the digit 0 or the digit one. Try string[i] = (n & 1) + '0';. Binary 0 and 1 are non-printing characters, so that's why you get no output.
#define INT_WIDTH 32
#define TEST 1
char *IntToBin(unsigned x, char *buffer) {
char *ptr = buffer + INT_WIDTH;
do {
*(--ptr) = '0' + (x & 1);
x >>= 1;
} while(x);
return ptr;
}
#if TEST
#include <stdio.h>
int main() {
int n;
char str[INT_WIDTH+1]; str[INT_WIDTH]='\0';
while(scanf("%d", &n) == 1)
puts(IntToBin(n, str));
return 0;
}
#endif

Arbitrary precision number to ascii in c [duplicate]

This question already has answers here:
Converting a big integer to decimal string
(4 answers)
Closed 5 years ago.
I've a uint8_t * array which contains an arbitrary precision number bigendian encoded.
I'd like to get its decimal Ascii representation. So I'd need to write a function that returns a char *.
The environment i'm using does not allow me to import any arbitrary precision library due to hardware limitations.
I'm sure there is something i can read to easily implement it.
For example the number defined by the following hex d53ceb9d32c6ca06 should be represented by 15365415089075571206.
Here's a method that should work. Note that it destructively modifies the bigint that you pass to it, so if you care about the value there, copy it to a temporary scratch buffer before calling the method.
Also, this isn't the most optimized version you could write, but if you're asking how to do it on here you probably aren't concerned yet about micro-optimization with this.
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
bool is_zero(uint8_t *bigi_data, int bigi_size) {
int i = 0;
while((i < bigi_size) && (bigi_data[i] == 0)) {
i++;
}
return (i >= bigi_size);
}
uint8_t bigdivmod(uint8_t *bigi_data, int bigi_size, uint8_t divisor) {
int i = 0;
uint16_t ans = 0;
while((i < bigi_size) && (bigi_data[i] == 0)) {
i++;
}
for (; i < bigi_size; i++) {
ans = ans*256 + bigi_data[i];
bigi_data[i] = ans / divisor;
ans = ans % divisor;
}
return (uint8_t)ans;
}
static const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
char *bigitoa(uint8_t *bigi_data, int bigi_size, char *out, int base) {
/* Assumes that "out" has enough room. DESTRUCTIVE TO BIGI, so copy */
/* if you care about the value */
/* Only really works for non-negative values */
int i = 0;
uint8_t swp;
int j;
if ((base < 2) || (base > 36)) {
return NULL;
}
if (is_zero(bigi_data, bigi_size)) {
out[0] = '0';
out[1] = '\0';
return out;
}
while (!is_zero(bigi_data, bigi_size)) {
out[i++] = digits[bigdivmod(bigi_data, bigi_size, base)];
}
out[i] = 0;
for (j = 0; j < i/2; j++) {
swp = out[i - 1 - j];
out[i - 1 - j] = out[j];
out[j] = swp;
}
return out;
}
int main(int argc, char *argv[]) {
uint8_t test_data[] = { 0xd5, 0x3c, 0xeb, 0x9d, 0x32, 0xc6, 0xca, 0x06 };
int test_data_len = sizeof(test_data);
char *p;
/* Times 3 because we can use three digits to represent 256. If changing */
/* the base below from "10", change this factor. */
p = malloc(3*test_data_len + 1);
printf("Test data works out to %s\n",
bigitoa(test_data, test_data_len, p, 10));
return 0;
}

How to convert ascii string to binary?

I'm trying to convert an ascii string to a binary string in C. I found this example Converting Ascii to binary in C but I rather not use a recursive function. I tried to write an iterative function as opposed to a recursive function, but the binary string is missing the leading digit. I'm using itoa to convert the string, however itoa is a non standard function so I used the implementation from What is the proper way of implementing a good "itoa()" function? , the one provided by Minh Nguyen.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int32_t ascii_to_binary(char *input, char **out, uint64_t len)
{
uint32_t i;
uint32_t str_len = len * 8;
if(len == 0)
{
printf("Length argument is zero\n");
return (-1);
}
(*out) = malloc(str_len + 1);
if((*out) == NULL)
{
printf("Can't allocate binary string: %s\n", strerror(errno));
return (-1);
}
if(memset((*out), 0, (str_len)) == NULL)
{
printf("Can't initialize memory to zero: %s\n", strerror(errno));
return (-1);
}
for(i = 0; i < len; i++)
itoa((int32_t)input[i], &(*out)[(i * 8)], 2);
(*out)[str_len] = '\0';
return (str_len);
}
int main(void)
{
int32_t rtrn = 0;
char *buffer = NULL;
rtrn = ascii_to_binary("a", &buffer, 1);
if(rtrn < 0)
{
printf("Can't convert string\n");
return (-1);
}
printf("str: %s\n", buffer);
return (0);
}
I get 1100001 for ascii character a, but I should get 01100001, so how do I convert the ascii string to the whole binary string?
You could change the for loop to something like this:
for(i = 0; i < len; i++) {
unsigned char ch = input[i];
char *o = *out + 8 * i;
int b;
for (b = 7; b >= 0; b--)
*o++ = (ch & (1 << b)) ? '1' : '0';
}
or similar:
for(i = 0; i < len; i++) {
unsigned char ch = input[i];
char *o = &(*out)[8 * i];
unsigned char b;
for (b = 0x80; b; b >>= 1)
*o++ = ch & b ? '1' : '0';
}
This program gets and integer ( which contains 32 bits ) and converts it to binary, Work on it to get it work for ascii strings :
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Best just write a simple function to do this using bitwise operators...
#define ON_BIT = 0x01
char *strToBin(char c) {
static char strOutput[10];
int bit;
/*Shifting bits to the right, but don't want the output to be in reverse
* so indexing bytes with this...
*/
int byte;
/* Add a nul at byte 9 to terminate. */
strOutput[8] = '\0';
for (bit = 0, byte = 7; bit < 8; bit++, byte--) {
/* Shifting the bits in c to the right, each time and'ing it with
* 0x01 (00000001).
*/
if ((c >> bit) & BIT_ON)
/* We know this is a 1. */
strOutput[byte] = '1';
else
strOutput[byte] = '0';
}
return strOutput;
}
Something like that should work, there's loads of ways you can do it. Hope this helps.

Print an int in binary representation using C

I'm looking for a function to allow me to print the binary representation of an int. What I have so far is;
char *int2bin(int a)
{
char *str,*tmp;
int cnt = 31;
str = (char *) malloc(33); /*32 + 1 , because its a 32 bit bin number*/
tmp = str;
while ( cnt > -1 ){
str[cnt]= '0';
cnt --;
}
cnt = 31;
while (a > 0){
if (a%2==1){
str[cnt] = '1';
}
cnt--;
a = a/2 ;
}
return tmp;
}
But when I call
printf("a %s",int2bin(aMask)) // aMask = 0xFF000000
I get output like;
0000000000000000000000000000000000xtpYy (And a bunch of unknown characters.
Is it a flaw in the function or am I printing the address of the character array or something? Sorry, I just can't see where I'm going wrong.
NB The code is from here
EDIT: It's not homework FYI, I'm trying to debug someone else's image manipulation routines in an unfamiliar language. If however it's been tagged as homework because it's an elementary concept then fair play.
Here's another option that is more optimized where you pass in your allocated buffer. Make sure it's the correct size.
// buffer must have length >= sizeof(int) + 1
// Write to the buffer backwards so that the binary representation
// is in the correct order i.e. the LSB is on the far right
// instead of the far left of the printed string
char *int2bin(int a, char *buffer, int buf_size) {
buffer += (buf_size - 1);
for (int i = 31; i >= 0; i--) {
*buffer-- = (a & 1) + '0';
a >>= 1;
}
return buffer;
}
#define BUF_SIZE 33
int main() {
char buffer[BUF_SIZE];
buffer[BUF_SIZE - 1] = '\0';
int2bin(0xFF000000, buffer, BUF_SIZE - 1);
printf("a = %s", buffer);
}
A few suggestions:
null-terminate your string
don't use magic numbers
check the return value of malloc()
don't cast the return value of malloc()
use binary operations instead of arithmetic ones as you're interested in the binary representation
there's no need for looping twice
Here's the code:
#include <stdlib.h>
#include <limits.h>
char * int2bin(int i)
{
size_t bits = sizeof(int) * CHAR_BIT;
char * str = malloc(bits + 1);
if(!str) return NULL;
str[bits] = 0;
// type punning because signed shift is implementation-defined
unsigned u = *(unsigned *)&i;
for(; bits--; u >>= 1)
str[bits] = u & 1 ? '1' : '0';
return str;
}
Your string isn't null-terminated. Make sure you add a '\0' character at the end of the string; or, you could allocate it with calloc instead of malloc, which will zero the memory that is returned to you.
By the way, there are other problems with this code:
As used, it allocates memory when you call it, leaving the caller responsible for free()ing the allocated string. You'll leak memory if you just call it in a printf call.
It makes two passes over the number, which is unnecessary. You can do everything in one loop.
Here's an alternative implementation you could use.
#include <stdlib.h>
#include <limits.h>
char *int2bin(unsigned n, char *buf)
{
#define BITS (sizeof(n) * CHAR_BIT)
static char static_buf[BITS + 1];
int i;
if (buf == NULL)
buf = static_buf;
for (i = BITS - 1; i >= 0; --i) {
buf[i] = (n & 1) ? '1' : '0';
n >>= 1;
}
buf[BITS] = '\0';
return buf;
#undef BITS
}
Usage:
printf("%s\n", int2bin(0xFF00000000, NULL));
The second parameter is a pointer to a buffer you want to store the result string in. If you don't have a buffer you can pass NULL and int2bin will write to a static buffer and return that to you. The advantage of this over the original implementation is that the caller doesn't have to worry about free()ing the string that gets returned.
A downside is that there's only one static buffer so subsequent calls will overwrite the results from previous calls. You couldn't save the results from multiple calls for later use. Also, it is not threadsafe, meaning if you call the function this way from different threads they could clobber each other's strings. If that's a possibility you'll need to pass in your own buffer instead of passing NULL, like so:
char str[33];
int2bin(0xDEADBEEF, str);
puts(str);
Here is a simple algorithm.
void decimalToBinary (int num) {
//Initialize mask
unsigned int mask = 0x80000000;
size_t bits = sizeof(num) * CHAR_BIT;
for (int count = 0 ;count < bits; count++) {
//print
(mask & num ) ? cout <<"1" : cout <<"0";
//shift one to the right
mask = mask >> 1;
}
}
this is what i made to display an interger as a binairy code it is separated per 4 bits:
int getal = 32; /** To determain the value of a bit 2^i , intergers are 32bits long**/
int binairy[getal]; /** A interger array to put the bits in **/
int i; /** Used in the for loop **/
for(i = 0; i < 32; i++)
{
binairy[i] = (integer >> (getal - i) - 1) & 1;
}
int a , counter = 0;
for(a = 0;a<32;a++)
{
if (counter == 4)
{
counter = 0;
printf(" ");
}
printf("%i", binairy[a]);
teller++;
}
it could be a bit big but i always write it in a way (i hope) that everyone can understand what is going on. hope this helped.
#include<stdio.h>
//#include<conio.h> // use this if you are running your code in visual c++, linux don't
// have this library. i have used it for getch() to hold the screen for input char.
void showbits(int);
int main()
{
int no;
printf("\nEnter number to convert in binary\n");
scanf("%d",&no);
showbits(no);
// getch(); // used to hold screen...
// keep code as it is if using gcc. if using windows uncomment #include & getch()
return 0;
}
void showbits(int n)
{
int i,k,andmask;
for(i=15;i>=0;i--)
{
andmask = 1 << i;
k = n & andmask;
k == 0 ? printf("0") : printf("1");
}
}
Just a enhance of the answer from #Adam Markowitz
To let the function support uint8 uint16 uint32 and uint64:
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// Convert integer number to binary representation.
// The buffer must have bits bytes length.
void int2bin(uint64_t number, uint8_t *buffer, int bits) {
memset(buffer, '0', bits);
buffer += bits - 1;
for (int i = bits - 1; i >= 0; i--) {
*buffer-- = (number & 1) + '0';
number >>= 1;
}
}
int main(int argc, char *argv[]) {
char buffer[65];
buffer[8] = '\0';
int2bin(1234567890123, buffer, 8);
printf("1234567890123 in 8 bits: %s\n", buffer);
buffer[16] = '\0';
int2bin(1234567890123, buffer, 16);
printf("1234567890123 in 16 bits: %s\n", buffer);
buffer[32] = '\0';
int2bin(1234567890123, buffer, 32);
printf("1234567890123 in 32 bits: %s\n", buffer);
buffer[64] = '\0';
int2bin(1234567890123, buffer, 64);
printf("1234567890123 in 64 bits: %s\n", buffer);
return 0;
}
The output:
1234567890123 in 8 bits: 11001011
1234567890123 in 16 bits: 0000010011001011
1234567890123 in 32 bits: 01110001111110110000010011001011
1234567890123 in 64 bits: 0000000000000000000000010001111101110001111110110000010011001011
Two things:
Where do you put the NUL character? I can't see a place where '\0' is set.
Int is signed, and 0xFF000000 would be interpreted as a negative value. So while (a > 0) will be false immediately.
Aside: The malloc function inside is ugly. What about providing a buffer to int2bin?
A couple of things:
int f = 32;
int i = 1;
do{
str[--f] = i^a?'1':'0';
}while(i<<1);
It's highly platform dependent, but
maybe this idea above gets you started.
Why not use memset(str, 0, 33) to set
the whole char array to 0?
Don't forget to free()!!! the char*
array after your function call!
Two simple versions coded here (reproduced with mild reformatting).
#include <stdio.h>
/* Print n as a binary number */
void printbitssimple(int n)
{
unsigned int i;
i = 1<<(sizeof(n) * 8 - 1);
while (i > 0)
{
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
/* Print n as a binary number */
void printbits(int n)
{
unsigned int i, step;
if (0 == n) /* For simplicity's sake, I treat 0 as a special case*/
{
printf("0000");
return;
}
i = 1<<(sizeof(n) * 8 - 1);
step = -1; /* Only print the relevant digits */
step >>= 4; /* In groups of 4 */
while (step >= n)
{
i >>= 4;
step >>= 4;
}
/* At this point, i is the smallest power of two larger or equal to n */
while (i > 0)
{
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < 32; ++i)
{
printf("%d = ", i);
//printbitssimple(i);
printbits(i);
printf("\n");
}
return 0;
}
//This is what i did when our teacher asked us to do this
int main (int argc, char *argv[]) {
int number, i, size, mask; // our input,the counter,sizeofint,out mask
size = sizeof(int);
mask = 1<<(size*8-1);
printf("Enter integer: ");
scanf("%d", &number);
printf("Integer is :\t%d 0x%X\n", number, number);
printf("Bin format :\t");
for(i=0 ; i<size*8 ;++i ) {
if ((i % 4 == 0) && (i != 0)) {
printf(" ");
}
printf("%u",number&mask ? 1 : 0);
number = number<<1;
}
printf("\n");
return (0);
}
the simplest way for me doing this (for a 8bit representation):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char *intToBinary(int z, int bit_length){
int div;
int counter = 0;
int counter_length = (int)pow(2, bit_length);
char *bin_str = calloc(bit_length, sizeof(char));
for (int i=counter_length; i > 1; i=i/2, counter++) {
div = z % i;
div = div / (i / 2);
sprintf(&bin_str[counter], "%i", div);
}
return bin_str;
}
int main(int argc, const char * argv[]) {
for (int i = 0; i < 256; i++) {
printf("%s\n", intToBinary(i, 8)); //8bit but you could do 16 bit as well
}
return 0;
}
Here is another solution that does not require a char *.
#include <stdio.h>
#include <stdlib.h>
void print_int(int i)
{
int j = -1;
while (++j < 32)
putchar(i & (1 << j) ? '1' : '0');
putchar('\n');
}
int main(void)
{
int i = -1;
while (i < 6)
print_int(i++);
return (0);
}
Or here for more readability:
#define GRN "\x1B[32;1m"
#define NRM "\x1B[0m"
void print_int(int i)
{
int j = -1;
while (++j < 32)
{
if (i & (1 << j))
printf(GRN "1");
else
printf(NRM "0");
}
putchar('\n');
}
And here is the output:
11111111111111111111111111111111
00000000000000000000000000000000
10000000000000000000000000000000
01000000000000000000000000000000
11000000000000000000000000000000
00100000000000000000000000000000
10100000000000000000000000000000
#include <stdio.h>
#define BITS_SIZE 8
void
int2Bin ( int a )
{
int i = BITS_SIZE - 1;
/*
* Tests each bit and prints; starts with
* the MSB
*/
for ( i; i >= 0; i-- )
{
( a & 1 << i ) ? printf ( "1" ) : printf ( "0" );
}
return;
}
int
main ()
{
int d = 5;
printf ( "Decinal: %d\n", d );
printf ( "Binary: " );
int2Bin ( d );
printf ( "\n" );
return 0;
}
Not so elegant, but accomplishes your goal and it is very easy to understand:
#include<stdio.h>
int binario(int x, int bits)
{
int matriz[bits];
int resto=0,i=0;
float rest =0.0 ;
for(int i=0;i<8;i++)
{
resto = x/2;
rest = x%2;
x = resto;
if (rest>0)
{
matriz[i]=1;
}
else matriz[i]=0;
}
for(int j=bits-1;j>=0;j--)
{
printf("%d",matriz[j]);
}
printf("\n");
}
int main()
{
int num,bits;
bits = 8;
for (int i = 0; i < 256; i++)
{
num = binario(i,bits);
}
return 0;
}
#include <stdio.h>
int main(void) {
int a,i,k=1;
int arr[32]; \\ taken an array of size 32
for(i=0;i <32;i++)
{
arr[i] = 0; \\initialised array elements to zero
}
printf("enter a number\n");
scanf("%d",&a); \\get input from the user
for(i = 0;i < 32 ;i++)
{
if(a&k) \\bit wise and operation
{
arr[i]=1;
}
else
{
arr[i]=0;
}
k = k<<1; \\left shift by one place evry time
}
for(i = 31 ;i >= 0;i--)
{
printf("%d",arr[i]); \\print the array in reverse
}
return 0;
}
void print_binary(int n) {
if (n == 0 || n ==1)
cout << n;
else {
print_binary(n >> 1);
cout << (n & 0x1);
}
}

Resources