Board index » jbuilder » What is the meaning and use of String.GetBytes()?

What is the meaning and use of String.GetBytes()?


2004-04-27 10:50:35 PM
jbuilder11
Thanks.
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)
 
 

Re:What is the meaning and use of String.GetBytes()?

Basically, it gives you (if possible) a byte representation of the Unicode
string. For example, if you wanted to transmit the string "Hello, world" to
an old-style TTY, you would convert it to bytes first before writing it to
the COM port as otherwise each character, being two bytes, would include a
null byte in the transmission.
From the String documentation:
Encodes this String into a sequence of bytes using the platform's default
charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the
default charset is unspecified. The CharsetEncoder class should be used when
more control over the encoding process is required.
Full details of the encoding process can be found at:
java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetEncoder.html
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"Raymond Kennington" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks.
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)
 

Re:What is the meaning and use of String.GetBytes()?

Kevin Dean wrote:
Quote

Basically, it gives you (if possible) a byte representation of the Unicode
string. For example, if you wanted to transmit the string "Hello, world" to
an old-style TTY, you would convert it to bytes first before writing it to
the COM port as otherwise each character, being two bytes, would include a
null byte in the transmission.

From the String documentation:
I've read the documentation and the info in 3 books. I am requesting the
meaning.
If 2 bytes turn into 1 byte, then a 40-character string would still produce a
40-byte encoding, not what appears to be 8 bytes when displayed.
Quote

Encodes this String into a sequence of bytes using the platform's default
charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the
default charset is unspecified. The CharsetEncoder class should be used when
more control over the encoding process is required.

Full details of the encoding process can be found at:

java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetEncoder.html
...
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)
 

{smallsort}

Re:What is the meaning and use of String.GetBytes()?

What do you mean by "If 2 bytes turn into 1 byte, then a 40-character string
would still produce a 40-byte encoding, not what appears to be 8 bytes when
displayed."? When I take a string and convert it to bytes as below, I get
the expected output of byte values equal to the string characters:
package getbytes;
public class GetBytes {
public static void main(String[] args) {
System.out.println("Hello, world");
byte[] bytes = "Hello, world".getBytes();
for (int index = 0; index < bytes.length; index++) {
System.out.print((char)bytes[index]);
}
System.out.println();
}
}
Output:
Hello, world
Hello, world
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"Raymond Kennington" < XXXX@XXXXX.COM >wrote in message
Quote
Kevin Dean wrote:
>
>Basically, it gives you (if possible) a byte representation of the
Unicode
>string. For example, if you wanted to transmit the string "Hello,
world" to
>an old-style TTY, you would convert it to bytes first before writing it
to
>the COM port as otherwise each character, being two bytes, would include
a
>null byte in the transmission.
>
>From the String documentation:

I've read the documentation and the info in 3 books. I am requesting the
meaning.

If 2 bytes turn into 1 byte, then a 40-character string would still
produce a
40-byte encoding, not what appears to be 8 bytes when displayed.

>
>Encodes this String into a sequence of bytes using the platform's
default
>charset, storing the result into a new byte array.
>
>The behavior of this method when this string cannot be encoded in the
>default charset is unspecified. The CharsetEncoder class should be used
when
>more control over the encoding process is required.
>
>Full details of the encoding process can be found at:
>
>
java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetEncoder.html
...
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)
 

Re:What is the meaning and use of String.GetBytes()?

Kevin Dean wrote:
Quote

What do you mean by "If 2 bytes turn into 1 byte, then a 40-character string
would still produce a 40-byte encoding, not what appears to be 8 bytes when
displayed."? When I take a string and convert it to bytes as below, I get
the expected output of byte values equal to the string characters:

package getbytes;

public class GetBytes {
public static void main(String[] args) {
System.out.println("Hello, world");
byte[] bytes = "Hello, world".getBytes();
for (int index = 0; index < bytes.length; index++) {
System.out.print((char)bytes[index]);
}
System.out.println();
}
}

Output:

Hello, world
Hello, world
This produces a different result:
System.out.println("Hello, world".getBytes());
and I want to know what it means.
--
Raymond Kennington
Programming Solutions
TeamW2W (InfoPower)
 

Re:What is the meaning and use of String.GetBytes()?

Raymond Kennington < XXXX@XXXXX.COM >writes:
Quote
This produces a different result:

System.out.println("Hello, world".getBytes());

and I want to know what it means.
getBytes() returns an array, and System.out.println(Object) - which is
the method ivoked - uses the passed object's toString() method. The
toString() in an array is the same as in Object. e.g. it gives the
class name ("[B" for a byte array) and the hashvalue.