• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null teminating a string

 
Dayanand Kangala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Have a basic doubt!
Consider we have a string of length 50 (8-bit) characters. If we store a value which has n (less than 50) characters, how do we "null-terminate" the string (for the remaining 50-n characters)?
Dayanand.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's really no need. You won't go past the last char of a String, unlike in C. If you want to shorten the String to fit, you can use trim().
[ September 11, 2002: Message edited by: Anthony Villanueva ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dayanand Kangala:
Consider we have a string of length 50 (8-bit) characters. If we store a value which has n (less than 50) characters, how do we "null-terminate" the string (for the remaining 50-n characters)?
Strings in Java are characters, not bytes so a String is made up of 16 bits. In Java it is not possible to create the scenario you are describing. We don't allocate Strings by length. We allocate Strings by the contents they hold. Java does not use null-terminated Strings. It actually uses a char array supported by a class to control the array.
 
Dayanand Kangala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Villanueva:
There's really no need. You won't go past the last char of a String, unlike in C. If you want to shorten the String to fit, you can use trim().
[ September 11, 2002: Message edited by: Anthony Villanueva ]

Correct. But, I have to interact with a legacy database which requires all records to be of fixed length!
I was told to terminate strings with less than field length with null characters ! I have currently appended "space"s to the value, but is this the same as appending null-characters?! (I am not sure if other legacy systems accessing the database would recognize the data if space is not the same as null characters).
 
Dayanand Kangala
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
Strings in Java are characters, not bytes so a String is made up of 16 bits. In Java it is not possible to create the scenario you are describing. We don't allocate Strings by length. We allocate Strings by the contents they hold. Java does not use null-terminated Strings. It actually uses a char array supported by a class to control the array.

Characters do have 16 bits in Java.
Consider the following code:
String str="example";
File f = new File("data.file");
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeBytes(str);
(or)
OutputStreamWriter osw = new OutputStreamWriter(dos, "UTF-8");
osw.write(str);
In both cases, each character in the string is stored as a byte (8-bits). So, it is possible for me to store and retieve characters (in 8-bit format). Am I making any mistake above?
Dayanand.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings in Java always contain 16bit characters. You can convert them to 8bit bytes as you did in your example.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this:
http://java.sun.com/docs/books/tutorial/java/data/cStrings.html
Java Strings are not compatible with C/C++ Strings. You may want to think about using a byte array instead and inserting a null character as the last entry in the array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic