文件中的字节数“0”。和“一个” [英] Number of bytes in file "zero" and "one"
问题描述
嘿朋友
我使用visual studio 2010,我有一个文件,如文件包含的输入流,例如(a,a,b,a,c,d,e)
-首先我想要计算文件的大小,(可能是文件是图像灰度)
- 更改二进制的ASCII字符?
-例如:
a in ASCII = 61
a in binary = 00111101
b in ASCII = 62 >
b in binary = 00111110
等...直到EOF
所以我需要帮助我可以计算:
零(a)= 3
一个数字(a)= 5
数字pf零(b)= 3
一个数字( b)= 5
....
thnx为你提供帮助
[更新]
最近尝试了什么:
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:
// 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]
我有什么尝试过:
我没试过!我不知道
[/update]
What I have tried:
I nothing tried ! I haven't idea
推荐答案
如果你用google搜索过,你应该找到一些像这样的教程文件i / o的介绍。对于初学者来说,更容易在Youtube上播放一些视频。
您还必须了解在文件api中您编写的字节 - 并且每种数据类型都有一些字节大小。 />
提示:想象一个文件自己作为字节数组,需要由你的代码解释。
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.
提示:为了转换a在二进制格式的字节中,您可以迭代地测试第7位然后向左移位数字,例如迭代以下代码
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
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
'转换过程'代码也可以计算收集的零(或一个)的总数。
[update]
您的 printStringAsBinary
不会迭代字符。尝试:
the 'conversion process' code could also count the total number of collected zeroes (or ones).
[update]
Your printStringAsBinary
doesn't iterate over characters. Try:
void printStringAsBinary(char* s)
{
while ( *s )
{
printCharAsBinary(*s);
printf("\n");
++s;
}
}
[/ update]
[另一个更新]
printCharAsBinary的修改允许计算字节中有多少个:
[/update]
[another update]
A modification of printCharAsBinary allows to count how many ones you have in the byte:
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]
这篇关于文件中的字节数“0”。和“一个”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!