question about some octal formatted output?

Discussion in 'Ruby' started by 7stud --, Oct 14, 2007.

  1. 7stud --

    7stud -- Guest

    eacute = ""
    eacute << 0xC3 << 0xA9 #eacute<< 195 << 169 ; or é

    p eacute

    --output:---
    "\303\251"

    That ouput is in octal--although there is no leading 0.

    1) Where does that format come from, i.e. no leading 0?
    2) Why is the output in octal and not hex?

    I looked up String#<< and it says it converts any Fixnum between 0-255
    to a character.

    3) Using what character set?


    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 14, 2007
    #1
    1. Advertising

  2. 7stud --

    Eric Hodel Guest

    On Oct 14, 2007, at 11:17 , 7stud -- wrote:
    > eacute =3D ""
    > eacute << 0xC3 << 0xA9 #eacute<< 195 << 169 ; or =E9
    >
    > p eacute
    >
    > --output:---
    > "\303\251"
    >
    > That ouput is in octal--although there is no leading 0.
    >
    > 1) Where does that format come from, i.e. no leading 0?
    > 2) Why is the output in octal and not hex?


    Its at least as old as C. You'll probably have to ask some really =20
    old timers for the answer.

    $ cat octal.c
    #include <stdio.h>

    void main() { printf("\303\251\n"); }
    $ gcc octal.c
    octal.c: In function 'main':
    octal.c:3: warning: return type of 'main' is not 'int'
    $ ./a.out
    =E9

    > I looked up String#<< and it says it converts any Fixnum between 0-255
    > to a character.
    >
    > 3) Using what character set?


    ASCII. Its your terminal that controls how it gets displayed. My =20
    terminal is set to UTF-8.

    --
    Poor workers blame their tools. Good workers build better tools. The
    best workers get their tools to do the work for them. -- Syndicate Wars
    Eric Hodel, Oct 14, 2007
    #2
    1. Advertising

  3. 7stud --

    mortee Guest

    7stud -- wrote:
    > eacute = ""
    > eacute << 0xC3 << 0xA9 #eacute<< 195 << 169 ; or é
    >
    > p eacute
    >
    > --output:---
    > "\303\251"
    >
    > That ouput is in octal--although there is no leading 0.
    >
    > 1) Where does that format come from, i.e. no leading 0?
    > 2) Why is the output in octal and not hex?
    >
    > I looked up String#<< and it says it converts any Fixnum between 0-255
    > to a character.
    >
    > 3) Using what character set?


    Actually, what's your problem with all that?

    Your ints specified in hex are actually converted to bytes in the
    string. That, interpreted as utf-8, may mean an é.

    The conventional syntax for specifying bytes by their integer value in
    string literals, used in C, shells and a number of other environments
    (including Ruby) is a backslash followed by octal digits. (The leading 0
    is used for specifying *integer* literals in octal.)

    String#inspect (which I guess p is using) adopts this syntax for
    displaying non-ascii and/or non-printing bytes in the string.

    I really don't get your third question. There's no character set
    involved here, beyond how you intended your two bytes to be interpreted.
    Those two bytes remain the same, regardless how they are displayed. They
    may mean two characters in plain old 8-bit charsets, they may mean e.g.
    one é in utf-8, or they may mean what p displays for them.

    mortee
    mortee, Oct 15, 2007
    #3
    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. Thomas Liesner

    newbie question concerning formatted output

    Thomas Liesner, Nov 29, 2005, in forum: Python
    Replies:
    11
    Views:
    435
    Thomas Liesner
    Nov 30, 2005
  2. dkk

    formatted input/output question

    dkk, Apr 10, 2006, in forum: C Programming
    Replies:
    2
    Views:
    312
    Herbert Rosenau
    Apr 12, 2006
  3. julien

    Regexp Octal question

    julien, Jun 25, 2007, in forum: Java
    Replies:
    6
    Views:
    1,142
    Roedy Green
    Jun 28, 2007
  4. Mateusz_madi

    Question about octal

    Mateusz_madi, Dec 31, 2010, in forum: C Programming
    Replies:
    13
    Views:
    628
    Hans Vlems
    Jan 4, 2011
  5. unbewust
    Replies:
    8
    Views:
    118
    Simon Krahnke
    Aug 15, 2007
Loading...

Share This Page