does eny1 nos how 2 convert 16 bit to 8 bit if so plz tell me, i need a c program for it. thanx

Recommended Answers

All 10 Replies

Be more specific.

Use a FILE to shave off the bits you don't need ;)

8 bit is to small to hold 16 bit, thats why they created 16 bit.

I need to convert binary, which are audio samples I have to make a program which converts binary to decimal which I have already done but I have to write another program which converts 16 bit to 8 bit

Are these 16-bit numbers signed or unsigned ?

For signed use x = x / 256;
For unsigned use x = x >> 8; (though x = x / 256 would do as well).

if u no how 2 write it plz write a c program 4 me as i dnt seem 2 understand how 2 write it. I have to ask a user to input a 16bit binary number which den be converted to a 8 bit number

http://www.catb.org/~esr/faqs/smart-questions.html#writewell
Sorry, but you're just going to have to demonstrate at least some programming skill as opposed to just faking it by thinking that writing in "kiddie speak" is good enough.

Like for example, prompt for a value, read in a value and display the value.
Then we can take it from there.

the following is my code and I am getting an error for this code I compiled it with cygwin and the error is "error while dumping state"

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 44000



int main()
{  
	FILE *sample,*s2;
	
   char x[16];
   long array[MAX] = {};
   int sum=0;
   int i=0;

   sample = fopen("samples.txt", "r");
   if (sample == NULL)
   {
       printf("Error\n");
   }
s2=fopen("16to8bit.txt","w");

	if(s2==NULL)
	{
		printf("\nError! Cannot open the ouput file!");
		exit(1);
	}
   

   for (i=0;fgets(x, 16, sample)!=NULL;i++)
   {
       printf("%s", x);
       array[i]=atol(x);
	
   }
   printf("\n");
   for(i=0;i<=MAX;i++)
   {
       printf("%d\n", array[i]);
   }
fclose(sample);
fclose(s2);

   printf("\nSuccess\n\n");

return 0;
}

Can any 1 help me with this code thanks

First: please use code tags. It makes you code readable.

the following is my code and I am getting an error for this code I compiled it with cygwin and the error is "error while dumping state"

The program compiles fine for me, or do you mean a runtime error? Since I do not have the files, I would guess that:

if (sample == NULL)
{
printf("Error\n");
}

You should insert a return 1; here, so the program quits when the file is non-existant.

and: for(i=0;i<=MAX;i++) This line means it will loop from 0 to 44000 which is 44001 times. But your array is only 44000 elemants, so this will cause an memory exception. Change it to: for(i=0;i<MAX;i++) regards Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.