Question:
why isn't this XORing in c?
mula
2008-10-17 14:31:37 UTC
//----------------while not the end of the infile----------------//
while((n = read(inFile, buffer, BUFFSIZE)) > 0)
{
unsigned rand_byte;
rand_byte = rand(); //sets rand_byte to random number
rand_byte = rand_byte % 256; //mods the random byte by 256
*buffer = *buffer^(unsigned char)rand_byte;
write(outFile, buffer, BUFFSIZE); //sets XORed byte to outFile
n++;
}
Six answers:
max
2008-10-17 14:40:22 UTC
You are missing a loop and you dont need to increase the value of n.

do it this way.



//----------------while not the end of the infile----------------//

while((n = read(inFile, buffer, BUFFSIZE)) > 0)

{

unsigned rand_byte;

rand_byte = rand(); //sets rand_byte to random number

rand_byte = rand_byte % 256; //mods the random byte by 256

for(int i=0; i< n; i++) {

buffer[i] = buffer[i]^(unsigned char)rand_byte;

}

write(outFile, buffer, n); //sets XORed byte to outFile

}
2008-10-17 21:40:34 UTC
What exactly is the purpose of this? It just produces a random stream of characters and writes them to a file. Anyway, I think the problem is that you are just XORing the first position in the buffer, not the whole thing. You have to loop the second two lines for each character in the buffer. You also should declare rand_byte as unsigned char, it works like an integer anyway (just 0-255).
Xjs
2008-10-17 21:52:57 UTC
This code snippet is very short and not very explainative. What is buffer allocated to? What is BUFFSIZE? What you are trying will only work if BUFFSIZE is 1, in this case, or only the first byte will be XOR-ed.



This might be the mistake, as I just compiled and ran the code here with BUFFSIZE = 1, and it worked as it seems to be intended.



Anyway, you should think about a for loop that iterates over buffer for each read(), because read()-s with bigger buffer sizes are more efficient.



By the way, you should initialize your random number generator by calling srand(seed) with a true random number as "seed". For the start, time(NULL) might be enough, but later on, you should use /dev/random or something.



If this all still doesn't help you, please refine your question. It *is* "XORing in C" for me, then ;-)



So long, Xjs.
Tizio 008
2008-10-17 22:19:08 UTC
the problem is

*buffer ^= (unsigned char)rand_byte;

here you xor just the first (what?) byte, not the rest.

loop on buffer[index] with a for loop e.g.



[unsigned ... rand_byte ... unsigned what?]



you can read single char instead (since single char reading func are already buffered ...), you could do:



int buffer;

while( (buffer=fgetc(inFile)) != EOF )

{

int rand_byte;

rand_byte = rand() % 256;

/* bad way to obtain a random number from 0 to 255...

but we do not concentrate on the random generation... */

buffer ^= rand_byte;

fputc(buffer, outFile);

}



even though there are things that can be obscure (e.g. why int rand_byte if all i want is a random byte?), this is clearer code...
2008-10-17 21:47:04 UTC
Wow, I have no idea.

But I do know that your BUFFSIZE needs to be 1.

Also, you're generating your random number for each character, and not storing it anywhere, so you're going to end up with useless random output!!



Let's try this:



#define BUFFSIZE (1)

unsigned rand_byte;

randomize();

rand_byte = rand(); //sets rand_byte to random number

rand_byte = rand_byte % 256; //mods the random byte by 256



while((n = read(inFile, buffer, BUFFSIZE)) > 0)

{

*buffer = *buffer^((unsigned char)rand_byte);

write(outFile, buffer, BUFFSIZE); //sets XORed byte to outFile

n++;

}
2008-10-17 21:40:03 UTC
Well one, you used the "=" sign in your while function. That means you're ASSIGNING n to the read function int. You need to use the "==" which is to test for equality. Also, you may want to run the randomize() function which changes the seeding number of rand(). Otherwise, you'll find that the rand() will spit out the same "random" numbers each time you run the program over.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...