• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check if character is null  RSS feed

 
Pieps Willemsen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayyy

Short question here. For an assignment we have to make a simple program that reads and encrypts text files. So what we made is a program that takes one text file and then encrypts it into another.
Now all fine and dandy thus far, but our method doesn't work. We're reading each character in the text file, and write them(after encrypting through a randomly generated chain) down in the new one. That's the most safe way to encrypt the file as it makes it more difficult to decrypt.

What we have is this statement to loop the process until there're no more characters to be found in the read textfile: , which quite frankly works when you only look at the problem superficially, but does not when you're stupid enough to open the actual text file.
The writing process does NOT stop using this while loop, the textfile gets immensely large(it got to 50mb at one point), which consisted of pretty much nothing but "[encryptedtext]???" etc.

How do we write a proper while loop that checks whether the next checked character is NOT null? I've had some pretty obvious suggestions like "make it s != ' '" or "make it check if the char is not -1", but we also want to encrypt potential "spacebar" spaces and even potential "double spacebar" spaces.

Thanks in advance!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're going to have to post more of your code so we can see what you're doing.
 
Paul Clapham
Sheriff
Posts: 22517
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought you wanted to encrypt the whole file? If that's the case then why don't you just read all the characters in the file and encrypt them? Trying to compare one of them to null... what is that supposed to be for?
 
Paul Clapham
Sheriff
Posts: 22517
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I don't understand why spaces are a problem, let alone what a "double space bar" space might be.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what exactly are you doing to encrypt the file? Did you write the encryption yourself, or are you using standard encryption algorithms? I'm assuming you wrote it yourself because the standard algorithms work on blocks of bytes, not chars.

And why do you expect the text file to end in a 'null' char with value 0? Unless you specifically write them to end with a '0' then they won't. C-strings in C/C++ are null-terminated, but that doesn't apply to text in general. The length of the file will be stored as meta-data in the file system journal, otherwise it would not know how long a binary file was.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How much do you know about Strings? In some languages, e.g. C, a String is not an object as such. It is a pointer to the first letter/char, and the text is assumed to continue until what is called the null character is found. You can read a bit about it in an article by Joel Spolsky but that article tells you a lot of other things too. A String in Java® is completely different, so different that there is even a Java® Language Specification (=JLS) section about it. That is one of the clearer parts of the JLS but it needs explanation.
In C you might have a memory location 0xabcd1234 which contains the value 0x0050 (=80 in decimal). You might have an array of chars like this:-
50:69:65:70:73:20:57:69;6c:6c:65:6d;7c:65:6d:00
… where each pair of numbers is a hexadecimal digit between 00 and ff (=255 in decimal). You can have a char[] which represents the same info. When you print out the first character in that array with the printf function the chip display the ASCII equivalent of each number, until it reaches the 00 which does not print. The 00 is called the null character and that style of String is called null‑terminated Strings or zero#x2011;terminated Strings. So I am actually writing about what you are interested in.

Things are totally different in Java®. A String is a full‑blown object and it incorporates a char[]. Remember that in Java® arrays are full‑blown objects too. A String object might hide a char[] like this:-
['P', 'i', 'e', 'p', 's', ' ', 'W', 'i', 'l', 'l', 'e', 'm', 's', 'e', 'n']
Note the array does not end with a null character or anything. Its last character is 'n'. By the way, Java® chars are different from in C, too. In C they occupy 8 bits and in Java® 16 bits. The array does have a .length field which can be used to tell how many characters it represents.

Now, the program assumes that your file which you are reading ends with a null character. You can test it more simply because a char is not a letter but a number:-
if (c == 0) ...
That is a big assumption about the structure of a text file, because they do not usually end with a null character. It suggests to me that you are doing something which I don't like. You are using a read() method something like this. The read() method reads one character at a time but as an int. It does not return a null character when the input finishes, but −1. And something else I don't like, namely putting a null character at the end of your text files. Please check very carefully that your encrypted file always ends with a null character. I think you should query this with your teacher. Please tell us what you are told.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!