Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey friends
I work with visual studio 2010 , I have a file like as input stream the file contains for example (a,a,b,a,c,d,e)
-firstly I want just calculate the size of the file , (maybe file was a image greyscale)
-change ASCII character in binary?

-for example :
a in ASCII =61
a in binary = 00111101
b in ASCII =62
b in binary =00111110
etc ... until EOF
so I need help for I can calculate:
number of zero (a)=3
number of one (a)=5
number pf zero (b)=3
NUMER of one (b)=5
....
thnx for ur help


[update]
What have tried more recently:
C++
// Make_Example.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <conio.h>

#include<stdlib.h>

 

void printCharAsBinary(char c) {
 
	int i;
 
	for(i = 0; i < 8; i++)
	{
 
	printf("%d", (c >> i) & 0x1);
 
	}
 
}
 

void printStringAsBinary(char* s){
	int j;
 
	for(j=0; j<4; j++)
	
	{
 
		printCharAsBinary(*s);
		printf("\n");	
 
	}
	printf("\n");
}
 
int _tmain(int argc, _TCHAR* argv[])
 
{
	char s1[] = "aabc";
 
	printStringAsBinary(s1);
	getch();
	
}

[/update]

What I have tried:

I nothing tried ! I haven't idea
Posted
Updated 30-May-16 7:05am
v2
Comments
F-ES Sitecore 30-May-16 6:26am    
Google "c# convert character to ascii" and "c# convert integer to binary"
yagami_md 30-May-16 6:31am    
friend c is not c#
and i want know how i can calculate the number de zero and the one !
F-ES Sitecore 30-May-16 6:49am    
sorry, thought this was tagged c#, my mistake. You can still find these answers easily by googling, use "c" instead of "c#"
George Jonsson 30-May-16 7:02am    
Just not that
a in binary = 0110 0001 (0x61)
b in binary = 0110 0010 (0x62)
See Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]
CPallini 30-May-16 13:06pm    
Your printStringAsBinary doesn't iterate over characters. See my update solution.

If you had googled you should had found some tutorials like this introduction to the file i/o. Easier can be some videos on Youtube for beginners.

You also must understand that in the file api you write bytes - and every data type has some byte size.

tip: Imagine a file yourself as byte array which needs to be interpreted by your code.
 
Share this answer
 
Hint: in order to convert a byte in binary format, you may, iteratively, test bit 7 and then shift left the number, e.g. iterate the following code

C
char binary[9];
binary[8] ='\0'; // NULL terminate the string
unsigned char a = 'a'; a is 0x61
binary[0] = (a & 0x80) ? '1' : '0';
a <<= 1;
binary[1] = (a & 0x80) ? '1' : '0';
a <<= 1;
//...
// put this in a loop


the 'conversion process' code could also count the total number of collected zeroes (or ones).

[update]
Your printStringAsBinary doesn't iterate over characters. Try:
C++
void printStringAsBinary(char* s)
{
  while ( *s )
  {
    printCharAsBinary(*s);
    printf("\n");
    ++s;
  }
}

[/update]


[another update]
A modification of printCharAsBinary allows to count how many ones you have in the byte:

C
int printCharAsBinary(char c) {

  int ones = 0;
  int i;

  for(i = 0; i < 8; i++)
  {
    char bit = (c & 0x80) ? 1 : 0;
    printf("%c", bit+'0');
    ones += bit;
    c <<= 1;
  }
  printf("\n");
  return ones;
}

[/another update]
 
Share this answer
 
v4
Comments
Patrice T 30-May-16 12:27pm    
Hi, your have a comment as Solution 3
CPallini 30-May-16 12:59pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900