-
Double-byte cahracters in a string!
Dear all.
How can I determine the actual length of a string, because I have to write
the string to an ASCII file with a fixed length.
thanks in advance!
-
Re: Double-byte cahracters in a string!
If you want to find the actual length of a string you use the length()
method. This gives you the length of the string in characters. "Writing...
to an ASCII file..." might involve converting the string to some other
format such as UTF-8 or it might not. If you're going to convert it to some
other format and want us to tell you how much space it will require to store
the converted string on disk, then we would need to know what kind of
conversion you plan to do.
Paul Wang <pwang@ptc.com> wrote in message news:394b79a5$1@news.devx.com...
>
> Dear all.
> How can I determine the actual length of a string, because I have to write
> the string to an ASCII file with a fixed length.
> thanks in advance!
-
Re: Double-byte cahracters in a string!
Thanks!
I have a data field that contain English and Chinese character togther, and
I want to write this data to an text file(sorry I'm not sure the converting
format, I think is "Big5".) with the fixed length.
For example:
String datafield = "abcX"; (X stands for Chinese character)
in java, the length of datafield is 4, but when I write it to the text file,
the length will be 5. I want to determine the actually length in java.
Is it clearly of my problem?
"Paul Clapham" <pclapham@core-mark.com> wrote:
>If you want to find the actual length of a string you use the length()
>method. This gives you the length of the string in characters. "Writing...
>to an ASCII file..." might involve converting the string to some other
>format such as UTF-8 or it might not. If you're going to convert it to
some
>other format and want us to tell you how much space it will require to store
>the converted string on disk, then we would need to know what kind of
>conversion you plan to do.
>
>Paul Wang <pwang@ptc.com> wrote in message news:394b79a5$1@news.devx.com...
>>
>> Dear all.
>> How can I determine the actual length of a string, because I have to write
>> the string to an ASCII file with a fixed length.
>> thanks in advance!
>
>
-
Re: Double-byte cahracters in a string!
I understand what you are doing. I don't understand why you need to know
the number of bytes you write to the file. Why not just write it to the
file?
But if you really want to know the length of what you are going to write,
try this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos, "BIG5"); // your
encoding here
osw.write(theString); // your string here
osw.flush();
int stringSize = baos.size(); // returns the number of bytes in the
converted stream
This doesn't write the string to the file, you will have to do that
separately.
Paul Wang <pwang@ptc.com> wrote in message news:394da270$1@news.devx.com...
>
> Thanks!
> I have a data field that contain English and Chinese character togther,
and
> I want to write this data to an text file(sorry I'm not sure the
converting
> format, I think is "Big5".) with the fixed length.
>
> For example:
> String datafield = "abcX"; (X stands for Chinese character)
> in java, the length of datafield is 4, but when I write it to the text
file,
> the length will be 5. I want to determine the actually length in java.
>
>
> Is it clearly of my problem?
>
> "Paul Clapham" <pclapham@core-mark.com> wrote:
> >If you want to find the actual length of a string you use the length()
> >method. This gives you the length of the string in characters.
"Writing...
> >to an ASCII file..." might involve converting the string to some other
> >format such as UTF-8 or it might not. If you're going to convert it to
> some
> >other format and want us to tell you how much space it will require to
store
> >the converted string on disk, then we would need to know what kind of
> >conversion you plan to do.
> >
> >Paul Wang <pwang@ptc.com> wrote in message
news:394b79a5$1@news.devx.com...
> >>
> >> Dear all.
> >> How can I determine the actual length of a string, because I have to
write
> >> the string to an ASCII file with a fixed length.
> >> thanks in advance!
> >
> >
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|