`cmd` and UTF-8

Discussion in 'Ruby' started by Antonio Mat, Aug 18, 2006.

  1. Antonio Mat

    Antonio Mat Guest

    I have spent several hours searching for this and I still don't know how
    it works.

    How is `cmd` string encoded? I want it in UTF-8, but I don't know how to
    do it!
    I have tried with $KCODE = 'u' but it doesn't works.
    My Ruby version is 1.8.2

    --
    Posted via http://www.ruby-forum.com/.
    Antonio Mat, Aug 18, 2006
    #1
    1. Advertising

  2. Antonio Mat

    Paul Battley Guest

    On 18/08/06, Antonio Mat <> wrote:
    > I have spent several hours searching for this and I still don't know how
    > it works.
    >
    > How is `cmd` string encoded? I want it in UTF-8, but I don't know how to
    > do it!
    > I have tried with $KCODE = 'u' but it doesn't works.
    > My Ruby version is 1.8.2


    Do you mean the return value? That depends on your platform, locale,
    and, possibly, the program itself. If you know the encoding, you can
    use Iconv to convert it to UTF-8:

    require 'iconv'
    s = `some cmd` # returns a string in iso-8859-1, for example
    iconv = Iconv.new("utf-8", "iso-8859-1")
    iconv.iconv(s) # => string in UTF-8

    Perhaps you could post a sample of code to show what you are trying to
    do. It's easier to solve a concrete problem than to read minds.

    Paul.
    Paul Battley, Aug 18, 2006
    #2
    1. Advertising

  3. Antonio Mat

    Antonio Mat Guest

    Paul Battley wrote:
    > On 18/08/06, Antonio Mat <> wrote:
    >> I have spent several hours searching for this and I still don't know how
    >> it works.
    >>
    >> How is `cmd` string encoded? I want it in UTF-8, but I don't know how to
    >> do it!
    >> I have tried with $KCODE = 'u' but it doesn't works.
    >> My Ruby version is 1.8.2

    >
    > Do you mean the return value? That depends on your platform, locale,
    > and, possibly, the program itself. If you know the encoding, you can
    > use Iconv to convert it to UTF-8:
    >
    > require 'iconv'
    > s = `some cmd` # returns a string in iso-8859-1, for example
    > iconv = Iconv.new("utf-8", "iso-8859-1")
    > iconv.iconv(s) # => string in UTF-8
    >
    > Perhaps you could post a sample of code to show what you are trying to
    > do. It's easier to solve a concrete problem than to read minds.


    Yes, I mean the return value.

    I can't use Iconv because the script should work with any locale.
    This is the code:

    require 'rexml/document'

    [...]

    include REXML

    @atom = Document.new
    entry = Element.new 'entry'
    entry.attributes['xmlns'] = 'http://www.w3.org/2005/Atom'
    @atom.elements << entry
    @atom << XMLDecl.new

    e = Element.new 'title'
    e << Text.new(`dcop amarok player title`)
    entry << e

    XML tag text should be in UTF-8.

    Thank you.



    --
    Posted via http://www.ruby-forum.com/.
    Antonio Mat, Aug 18, 2006
    #3
  4. > Yes, I mean the return value.
    >
    > I can't use Iconv because the script should work with any locale.


    Until Ruby gets some non-basic Unicode support, I -think- you're stuck
    to iconv-ing. I don't think that even $KCODE='u' will detect user input
    locale and convert automatically, but only changes the behaviour of
    string manipulation code. I could be completely wrong though.

    You should be able to detect the current locale using the standard
    library, I haven't really done this though. Welcome to the world of text
    encoding, may whoever invented codepages burn in hell forever for that.

    David Vallner
    David Vallner, Aug 18, 2006
    #4
  5. Antonio Mat

    Guest

    Hi,

    At Sat, 19 Aug 2006 04:04:03 +0900,
    Antonio Mat wrote in [ruby-talk:209287]:
    > I can't use Iconv because the script should work with any locale.


    Then what encoding does your target program use?

    If it is affected by environment locale, it may use utf-8 by
    setting ENV["LC_ALL"] etc.

    --
    Nobu Nakada
    , Aug 19, 2006
    #5
  6. Antonio Mat

    Antonio Mat Guest

    Antonio Mat wrote:
    > I have spent several hours searching for this and I still don't know how
    > it works.
    >
    > How is `cmd` string encoded? I want it in UTF-8, but I don't know how to
    > do it!
    > I have tried with $KCODE = 'u' but it doesn't works.
    > My Ruby version is 1.8.2


    Here is a solution:

    ENV["LC_ALL"] = "C.UTF-8"
    utf8_string = `cmd`

    Thank you all!

    --
    Posted via http://www.ruby-forum.com/.
    Antonio Mat, Aug 21, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,034
    Peter Otten
    Jun 3, 2005
  2. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    353
    Bengt Richter
    Jun 26, 2005
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    370
    Scott David Daniels
    Mar 8, 2006
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    537
    Diez B. Roggisch
    May 29, 2008
  5. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    328
    Michele Simionato
    May 29, 2008
Loading...

Share This Page