6 Replies Latest reply: Jul 28, 2010 8:04 AM by 794822 RSS

    bytebuffer  and how to convert to String ?

    794822
      For example my bytebuffer has the values 72. 69, 76,76,79 ( HELLO)
      String.value(bytebuffer.array()); does not work ( compiler treats bytebuffer.array() as an object)

      Any idea ?

      Thanks
        • 1. Re: bytebuffer  and how to convert to String ?
          843790
          new String(bytebuffer.array(), "ASCII");
          .

          Note that you CAN'T correctly convert a byte array to a String without knowing its encoding. Read [_The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)_|http://www.joelonsoftware.com/articles/Unicode.html] for details.
          • 2. Re: bytebuffer  and how to convert to String ?
            794822
            thank you !
            My file is ANSI coded (pure ascii, isn't it ?)
            • 3. Re: bytebuffer  and how to convert to String ?
              843790
              tonnot wrote:
              My file is ANSI coded (pure ascii, isn't it ?)
              Nope, ANSI is not the same as ASCII.

              ANSI is not a well-specified encoding at all, in fact.

              Usually "ANSI" is used to refer to "whatever the platform default encoding is on this Windows machine". So if you move a text file in "ANSI" encoding from an english Windows installation to another Windows installation that's set to use a russian locale, then "ANSI" will mean something different on those two machines and therefore the decoded String won'T be correct (unless you're lucky and your file only contains characters from the ASCII-subset (i.e. the lower 128 values)).

              On English-language Windows installations, ANSI usually refers to [Windows-1252|http://en.wikipedia.org/wiki/Windows-1252].
              • 4. Re: bytebuffer  and how to convert to String ?
                794822
                in a simple file written with notepad and ANSI write option ....
                if I write ñ ( the spanish n with upper sign) and give you the file... what do you see ?
                • 5. Re: bytebuffer  and how to convert to String ?
                  843790
                  tonnot wrote:
                  in a simple file written with notepad and ANSI write option ....
                  if I write ñ ( the spanish n with upper sign) and give you the file... what do you see ?
                  If I happen to have the same locale setting as you and open it in notepad, I'll see the ñ.
                  If I have any other setting, I'll see another character that's got the same numeric value in the encoding of my locale.

                  If you also tell me the real encoding (for example windows-1252) and I use that information to load the file with a sane program (i.e. one that actually knows and cares about encoding), then I'll see the ñ.
                  • 6. Re: bytebuffer  and how to convert to String ?
                    794822
                    ok, thank you very much