C 为什么以下两个函数中只有一个打印正确的转换:文本文件到16位和8位显示器?
为什么“我的程序”主函数中的以下两个函数只有一个将文本文件(其中只有单个字符“e”)正确转换为该字符“e”的16位和8位显示?例如,它只打印:“e”=101 101=011001000000000 0000000000000000 0=00000000 00000000 00000000 00000000C 为什么以下两个函数中只有一个打印正确的转换:文本文件到16位和8位显示器?,c,arrays,prototype,endianness,C,Arrays,Prototype,Endianness,为什么“我的程序”主函数中的以下两个函数只有一个将文本文件(其中只有单个字符“e”)正确转换为该字符“e”的16位和8位显示?例如,它只打印:“e”=101 101=011001000000000 0000000000000000 0=00000000 00000000 00000000 00000000 #include<stdio.h> #include<stdlib.h> void displaySixteenBits(char *value );//protot
#include<stdio.h>
#include<stdlib.h>
void displaySixteenBits(char *value );//prototype
void displayEightBits( char *value );//prototype
int main(void)
{
FILE *ptr_file;
char buf[1000];
ptr_file = fopen("input.txt","r");
if (!ptr_file)
return 1;
while (fgets(buf,1000, ptr_file)!=NULL)
printf("The file read: \t");
printf("%s\n",buf);
/*Only one of the following two lines of code prints*/
displaySixteenBits( buf );
displayEightBits( buf );
fclose(ptr_file);
return 0;
}//end main
/* Function to display text file character to 16 bits*/
void displaySixteenBits( char *value )
{
char c;
int displayMask = 1 << 31;
printf( "%10u = ", *value );
for ( c = 1; c <= 32; ++c ) {
putchar( *value & displayMask ? '1' : '0' );
*value <<= 1;
if (c % 16 == 0 ){
putchar( ' ' );
}
}
putchar( '\n' );
}//end display sixteen bits
/* Function to display text file character to eight bits*/
void displayEightBits( char *value )
{
char c;
int displayMask = 1 << 31;
printf( "%10u = ", *value );
for ( c = 1; c <= 32; ++c ) {
putchar( *value & displayMask ? '1' : '0' );
*value <<= 1;
if (c % 8 == 0 ){
putchar( ' ' );
}
}
putchar( '\n' );
}//end display eight bits
应改为:
‘e’=101
101=011001000000000 0000000000000000
101=01100100000000 00000000 00000000
#include<stdio.h>
#include<stdlib.h>
void displaySixteenBits(char *value );//prototype
void displayEightBits( char *value );//prototype
int main(void)
{
FILE *ptr_file;
char buf[1000];
ptr_file = fopen("input.txt","r");
if (!ptr_file)
return 1;
while (fgets(buf,1000, ptr_file)!=NULL)
printf("The file read: \t");
printf("%s\n",buf);
/*Only one of the following two lines of code prints*/
displaySixteenBits( buf );
displayEightBits( buf );
fclose(ptr_file);
return 0;
}//end main
/* Function to display text file character to 16 bits*/
void displaySixteenBits( char *value )
{
char c;
int displayMask = 1 << 31;
printf( "%10u = ", *value );
for ( c = 1; c <= 32; ++c ) {
putchar( *value & displayMask ? '1' : '0' );
*value <<= 1;
if (c % 16 == 0 ){
putchar( ' ' );
}
}
putchar( '\n' );
}//end display sixteen bits
/* Function to display text file character to eight bits*/
void displayEightBits( char *value )
{
char c;
int displayMask = 1 << 31;
printf( "%10u = ", *value );
for ( c = 1; c <= 32; ++c ) {
putchar( *value & displayMask ? '1' : '0' );
*value <<= 1;
if (c % 8 == 0 ){
putchar( ' ' );
}
}
putchar( '\n' );
}//end display eight bits
#包括
#包括
无效显示十六位(字符*值)//原型
无效显示八位(字符*值)//原型
内部主(空)
{
文件*ptr_文件;
char-buf[1000];
ptr_file=fopen(“input.txt”,“r”);
如果(!ptr_文件)
返回1;
while(fgets(buf,1000,ptr_文件)!=NULL)
printf(“文件读取:\t”);
printf(“%s\n”,buf);
/*仅打印以下两行代码中的一行*/
显示十六个字节(buf);
显示八比特(buf);
fclose(ptr_文件);
返回0;
}//端干管
/*函数将文本文件字符显示为16位*/
无效显示十六位(字符*值)
{
字符c;
int displayMask=1int displayMask=1从您的帖子中不清楚输入文件和预期输出的确切内容。