🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #7717

Re: tabla de caracteres ascii

#7717
From: Pascal Bourguignon
Date: Sun, 09 Feb 2003 19:11
371 lines
13910 bytes
--=-=-=
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit

Fernando Arbeiza <arbeizaf@ono.com> writes:

> Hola:
> 
> On 9 Feb 2003 03:23:40 -0800, carleto <pedgima@latinmail.com> wrote:
> >  for(i=0; i<256; i++){
> >  printf("%c,%d ", i, i);
> >  }
> > 
> > ocurre lo siguiente, no me imprime los caracteres del 128 al 159, en
> > todos esos pone el mismo dibujo, y cuando i llega a 204 produce una
> > violacion de segmento, �por que ocurre eso?, �no puedo usar la tabla
> > de caracteres ascii en linux?
 

--=-=-=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

No s�, pero los caracteres  que se dibujan dependen del locale. Quizas
tienes un  local particular en el  cual se busca  un caracter especial
por el 204, y hay un problema con las fuentes...


--=-=-=
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit


> Lo primero, tienes una ayuda en:
> 
>     man ascii
>     man iso_8859_1
> 
> Me parece que los c�digos del 128 al 159 no son imprimibles, luego es
> l�gico que no aparezcan. Lo que no tengo ni idea es para qu� se usan.

--=-=-=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit


Son caracteres de control como los del  0 al 31.  Por si a caso se usa
una l�nea 7-bit con paridad...

--=-=-=
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit


 
> La verdad es que no se me ocurre por qu� hay una violaci�n de segmento.
> El �nico problema que le veo a tu c�digo es si i lo has declarado como
> char y resulte que char tenga signo. Pero entonces estar�as en un bucle
> infinito, no en una violaci�n de segmento.
> 
> De todas formas te pongo un c�digo que deber�a funcionar:
> 
> ##########
> #include <stdio.h>
> #include <limits.h>
> 
> int main(void) {
>     char i;

Asi que, signed char, ie: -128 .. 127.

>     for (i = CHAR_MIN; i < CHAR_MAX; i++) {

--=-=-=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit


Asi que no se imprime el ultimo (CHAR_MAX). �No hay derecho!

>         printf("%c, %d  ", i, (int)i);
>     }
> 
>     return 0;
> }
> ##########
> 
> Un saludo.
> -- 
> Fernando Arbeiza        <URL: mailto:arbeizaf@ono.com>
> Crea tu propio Linux:   <URL: http://www.escomposlinux.org/lfs-es>

[pascal@thalassa pas-graves]$ LC_ALL=es_ES  ascii -d -n -c -a -C -i

  0 NUL   1 SOH   2 STX   3 ETX   4 EOT   5 ENQ   6 ACK   7 BEL 
  8 BS    9 HT   10 LF   11 VT   12 FF   13 CR   14 SO   15 SI  
 16 DLE  17 DC1  18 DC2  19 DC3  20 DC4  21 NAK  22 SYN  23 ETB 
 24 CAN  25 EM   26 SUB  27 ESC  28 FS   29 GS   30 RS   31 US  
 32 SP   33  !   34  "   35  #   36  $   37  %   38  &   39  '  
 40  (   41  )   42  *   43  +   44  ,   45  -   46  .   47  /  
 48  0   49  1   50  2   51  3   52  4   53  5   54  6   55  7  
 56  8   57  9   58  :   59  ;   60  <   61  =   62  >   63  ?  
 64  @   65  A   66  B   67  C   68  D   69  E   70  F   71  G  
 72  H   73  I   74  J   75  K   76  L   77  M   78  N   79  O  
 80  P   81  Q   82  R   83  S   84  T   85  U   86  V   87  W  
 88  X   89  Y   90  Z   91  [   92  \   93  ]   94  ^   95  _  
 96  `   97  a   98  b   99  c  100  d  101  e  102  f  103  g  
104  h  105  i  106  j  107  k  108  l  109  m  110  n  111  o  
112  p  113  q  114  r  115  s  116  t  117  u  118  v  119  w  
120  x  121  y  122  z  123  {  124  |  125  }  126  ~  127 DEL 
128 NUL 129 SOH 130 STX 131 ETX 132 EOT 133 ENQ 134 ACK 135 BEL 
136 BS  137 HT  138 LF  139 VT  140 FF  141 CR  142 SO  143 SI  
144 DLE 145 DC1 146 DC2 147 DC3 148 DC4 149 NAK 150 SYN 151 ETB 
152 CAN 153 EM  154 SUB 155 ESC 156 FS  157 GS  158 RS  159 US  
160  �  161  �  162  �  163  �  164  �  165  �  166  �  167  �  
168  �  169  �  170  �  171  �  172  �  173  �  174  �  175  �  
176  �  177  �  178  �  179  �  180  �  181  �  182  �  183  �  
184  �  185  �  186  �  187  �  188  �  189  �  190  �  191  �  
192  �  193  �  194  �  195  �  196  �  197  �  198  �  199  �  
200  �  201  �  202  �  203  �  204  �  205  �  206  �  207  �  
208  �  209  �  210  �  211  �  212  �  213  �  214  �  215  �  
216  �  217  �  218  �  219  �  220  �  221  �  222  �  223  �  
224  �  225  �  226  �  227  �  228  �  229  �  230  �  231  �  
232  �  233  �  234  �  235  �  236  �  237  �  238  �  239  �  
240  �  241  �  242  �  243  �  244  �  245  �  246  �  247  �  
248  �  249  �  250  �  251  �  252  �  253  �  254  �  255  �  


------------------------------------------------------------------------
/******************************************************************************
FILE:               ascii.c
LANGUAGE:           ANSI-C
SYSTEM:             ANSI
USER-INTERFACE:     ANSI
DESCRIPTION
    This program prints character tables.
AUTHOR
    <PJB> Pascal J. Bourguignon
MODIFICATIONS
    1991-04-30 <PJB> Creation.
    1993-11-23 <PJB> Added -C option.  
    1994-02-15 <PJB> Added RCS keywords.
    1994-07-13 <PJB> Transformed C++ comments into C comments.
    1995-09-08 <PJB> Some changes.
    1995-11-04 <PJB> Use unsigned char instead of char for array subscripts.
    2001-04-27 <PJB> Added to CVS.
    2001-04-30 <PJB> Initial entry into CVS.
    2002-08-12 <PJB> Updated for new common/makedir from MapTree.
    2003-01-07 <PJB> Cleaned-up the options and orthogonalized them.
                     Use of ctype macros to filter the characters
                     (so this may be locale dependent too).
LEGAL
    GPL
    Copyright Pascal J. Bourguignon 1991 - 2003

    This file is part of PJB ASCII UTILITIES.

    This  program is  free software;  you can  redistribute  it and/or
    modify it  under the  terms of the  GNU General Public  License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program  is distributed in the  hope that it  will be useful,
    but  WITHOUT ANY WARRANTY;  without even  the implied  warranty of
    MERCHANTABILITY or FITNESS FOR  A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a  copy of the GNU General Public License
    along with  this program; see the  file COPYING; if  not, write to
    the Free  Software Foundation, Inc.,  59 Temple Place,  Suite 330,
    Boston, MA 02111-1307 USA
******************************************************************************/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <locale.h>
    

/*
    printables:
    -----------
    print the hex code                         -h
    print the dec code                         -d 
    print the oct code                         -o
    print the name      (ETX for 0x03)         -n  (for printable = -l)
    print the notation  (^C  for 0x03)         -N  (for printable = -l)
    print the character literal                -l
    print raw (without nice formating)         -r
    print all                                  -A

    categories of characters:
    --------------------------
    control characters <32 or DEL              -c
    ascii characters between 32 and 127        -a
    control characters between 0x80 and 0x9f   -C
    iso characters between 0xa0 and 0xff       -i

    default : -a
    shortcut: -e = -a -i
*/

    void usage(char* name,const char* options)
    {
        printf("%s usage:\n\t%s %s\n",name,name,options);
    }/*usage;*/
    

    static char*    asciinames[]={
        "NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL",
        "BS ","HT ","LF ","VT ","FF ","CR ","SO ","SI ",
        "DLE","DC1","DC2","DC3","DC4","NAK","SYN","ETB",
        "CAN","EM ","SUB","ESC","FS ","GS ","RS ","US ",
        "SP ","DEL",
        NULL};

    void  print_char(int length,int hex,int dec,int oct,int name,
                 int notation,int literal,int all_chars,int i)
    {
        if(hex){ printf("%02x ",i); }
        if(dec){ printf("%3d ",i); }
        if(oct){ printf("%03o ",i); }
        if(name){ 
            if(all_chars||isgraph(i)){ 
                printf(" %c  ",i);  
                /* NOTE: iso-latin-1 char have names like Aacute... */
            }else if(i==127){
                printf("DEL ");
            }else{
                printf("%3s ",asciinames[0x7f&i]);
            }
        }
        if(notation){
            if(iscntrl(i)){
                printf("^%c ",'@'+i);
            }else{
                printf(" %c ",i);
            }
        }
        if(literal){
            printf("%c",i);
        }
    }/*print_char*/


int main(int argc,char** argv)
{
    int             i;
    int             length;
    int             modulo;

    /*
        (generate-options '((-h print_hex) (-d print_dec) (-o print_oct)
        (-n print_name) (-N print_notation) (-l print_literal) (-r print_raw)
        (-c controls) (-a ascii) (-C iso_controls) (-i iso) (-e ascii iso)
        (-A all_chars))
        '())

    */

    const char usage_options[]="[-h] [-d] [-o] [-n] [-N] [-l] [-r] [-c] [-a] [-C] [-i] [-e] [-A] ";

    int         ascii           =0;
    int         controls        =0;
    int         iso             =0;
    int         iso_controls    =0;
    int         all_chars       =0;
    int         print_dec       =0;
    int         print_hex       =0;
    int         print_literal   =0;
    int         print_name      =0;
    int         print_notation  =0;
    int         print_oct       =0;
    int         print_raw       =0;

    for(i=1;i<argc;i++){
        if(strcmp(argv[i],"-h")==0){          print_hex=1;
        }else if(strcmp(argv[i],"-d")==0){    print_dec=1;
        }else if(strcmp(argv[i],"-o")==0){    print_oct=1;
        }else if(strcmp(argv[i],"-n")==0){    print_name=1;
        }else if(strcmp(argv[i],"-N")==0){    print_notation=1;
        }else if(strcmp(argv[i],"-l")==0){    print_literal=1;
        }else if(strcmp(argv[i],"-r")==0){    print_raw=1;
        }else if(strcmp(argv[i],"-c")==0){    controls=1;
        }else if(strcmp(argv[i],"-a")==0){    ascii=1;
        }else if(strcmp(argv[i],"-C")==0){    iso_controls=1;
        }else if(strcmp(argv[i],"-i")==0){    iso=1;
        }else if(strcmp(argv[i],"-e")==0){
            ascii=1;
            iso=1;
        }else if(strcmp(argv[i],"-A")==0){    all_chars=1;
        }else{
            usage(argv[0],usage_options);
            exit(1);
        }
    }


    if(argc<=1){ /* if no option is given then default is -a */
        ascii=1; 
        print_name=1;
    }
    if(print_hex+print_dec+print_oct+print_name
       +print_notation+print_literal==0){
        if(all_chars){
            print_raw=1;
        }else{
            print_name=1;
        }
    }
    setlocale(LC_ALL, "" );

    if(print_raw){
        for(i=0;i<256;i++){
            if(all_chars
               ||(controls && (i<128) && iscntrl(i))
               ||(iso_controls && (128<=i) && iscntrl(i))
               ||(ascii && (i<128) && isprint(i))
               ||(iso && (128<=i) && isprint(i))){
                printf("%c",i);
            }
        }
        exit(0);
    }


    length=0;
    if(print_hex){      length+=3; }
    if(print_dec){      length+=4; }
    if(print_oct){      length+=4; }
    if(print_name){     length+=4; }
    if(print_notation){ length+=3; }
    if(print_literal){  length+=2; }
    if(16*length<=80){
        modulo=16;
    }else if(8*length<=80){
        modulo=8;
    }else if(4*length<=80){
        modulo=4;
    }else{
        modulo=2;
    }

    for(i=0;i<256;i++){
        if((i%modulo)==0){
            printf("\n");
        }
        if(all_chars
           ||(controls && (i<128) && iscntrl(i))
           ||(iso_controls && (128<=i) && iscntrl(i))
           ||(ascii && (i<128) && isprint(i))
           ||(iso && (128<=i) && isprint(i))){
            print_char(length,print_hex,print_dec,print_oct,print_name,
                       print_notation,print_literal,all_chars,i);
        }else{
            printf("%*s",length,"");
        }
    }
    printf("\n");
    return(0);
}/*main*/



------------------------------------------------------------------------

-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
There is a fault in reality. Do not adjust your minds. -- Salman Rushdie

--=-=-=--
Technical Details
Message-ID: <87of5l2rrh.fsf@thalassa.informatimago.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!thalassa.informatimago.COM!not-for-mail
References: <df729253.0302090323.73c24a76@posting.google.com> <slrnb4chgp.f1j.arbeizaf@jupiter.olimpo>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,c6a8b168f34336af
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-02-09 11:11:53 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!thalassa.informatimago.COM!not-for-mail
From: Pascal Bourguignon <pjb@informatimago.com>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: tabla de caracteres ascii
Date: 09 Feb 2003 20:11:46 +0100
Organization: informatimago.com
Lines: 371
Sender: pascal@thalassa.informatimago.com
Message-ID: <87of5l2rrh.fsf@thalassa.informatimago.com>
References: <df729253.0302090323.73c24a76@posting.google.com> <slrnb4chgp.f1j.arbeizaf@jupiter.olimpo>
NNTP-Posting-Host: thalassa.informatimago.com (212.87.205.57)
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Trace: fu-berlin.de 1044817911 42991913 212.87.205.57 (16 [41911])
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2