Ascii Characters

Discussion in 'C++' started by e4me4m@gmail.com, Apr 28, 2009.

  1. Guest

    This is a very dumb question... I have a std::vector<std::string> that
    I'm pushing ascii charcters into like so:

    set.push_back("a");
    set.push_back("b");
    ....
    ....

    I'm doing this manually. This is error prone and time consuming. Does
    something in the STL have these already? I'm rather new to C++ so
    forgive me if this is blatantly obvious. Google did not help much. The
    goal is to get all typable ascii chars into the vector.

    Thanks
     
    , Apr 28, 2009
    #1
    1. Advertising

  2. wrote:
    > This is a very dumb question... I have a std::vector<std::string> that
    > I'm pushing ascii charcters into like so:
    >
    > set.push_back("a");
    > set.push_back("b");


    'set' is not the best name for a vector. There is 'std::set', you know...

    > ...
    > ...
    >
    > I'm doing this manually. This is error prone and time consuming. Does
    > something in the STL have these already? I'm rather new to C++ so
    > forgive me if this is blatantly obvious. Google did not help much. The
    > goal is to get all typable ascii chars into the vector.


    I am not sure what is "a typable ascii", to be honest, perhaps you need
    to do 'isprint' on those, but look into 'std::generate' or 'std::fill'.
    There is a constructor of 'std::string' that takes the value and the
    count, use it in a loop. Something like

    for (char c = ' '; c <= '~'; ++c)
    set.push_back(std::string(1, c));

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 28, 2009
    #2
    1. Advertising

  3. writes:

    > This is a very dumb question... I have a std::vector<std::string> that
    > I'm pushing ascii charcters into like so:
    >
    > set.push_back("a");
    > set.push_back("b");
    > ...
    >
    > I'm doing this manually. This is error prone and time consuming. Does
    > something in the STL have these already? I'm rather new to C++ so
    > forgive me if this is blatantly obvious. Google did not help much. The
    > goal is to get all typable ascii chars into the vector.


    Better browse directly a C++ Library Reference such as
    http://www.cplusplus.com/reference/


    std::string printableCharactersInASCII(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");

    for(std::string::const_iterator it=printableCharactersInASCII.begin();
    it!=printableCharactersInASCII.end();
    it++){
    std::string character(it,it+1);
    set.push_back(character);
    }

    Notice that this program works even on systems that don't use the
    ASCII _code_, as long as they can represent all the printable
    characters of the ASCII _character_ _set_ (those in that
    printableCharactersInASCII string).


    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 28, 2009
    #3
  4. Victor Bazarov <> writes:

    > for (char c = ' '; c <= '~'; ++c)
    > set.push_back(std::string(1, c));


    This doesn't work on my EBCDIC based system. Broken program!

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 28, 2009
    #4
  5. Pascal J. Bourguignon wrote:
    > Victor Bazarov <> writes:
    >
    >> for (char c = ' '; c <= '~'; ++c)
    >> set.push_back(std::string(1, c));

    >
    > This doesn't work on my EBCDIC based system. Broken program!


    Didn't the OP say "ASCII"?...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 28, 2009
    #5
  6. Victor Bazarov <> writes:

    > Pascal J. Bourguignon wrote:
    >> Victor Bazarov <> writes:
    >>
    >>> for (char c = ' '; c <= '~'; ++c)
    >>> set.push_back(std::string(1, c));

    >>
    >> This doesn't work on my EBCDIC based system. Broken program!

    >
    > Didn't the OP say "ASCII"?...


    No, he said "the ASCII characters".

    Whatever the encoding used by the programming language and/or the
    host, you can handle them as long has their character set includes the
    ASCII printable characters, which are the space and
    !"#$%&'()*+,-./0123456789:;<=>?
    @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
    `abcdefghijklmnopqrstuvwxyz{|}~


    Unfortunately, when you use the char type in C or C++, with literals
    such as ' ' or '~', you are not working with characters anymore, but
    with their codes (char is a subtype of int, not of character).


    We may assume that if the OP is interested by ASCII characters, he may
    have to encode and decode them, so it is assumed that the
    printableCharactersInASCII string will be used to do that:

    #include <limits.h>
    #if CHARBITS<7
    typedef int ASCII_Code;
    #else
    typedef unsigned char ASCII_Code;
    #endif

    ASCII_Code character_to_ASCII_Code(std::string character){
    if(character=="\n"){
    return(10);
    }else{
    size_t pos=printableCharactersInASCII.find(character);
    if(pos==std::string::npos){
    throw std::exception("Not an ASCII character");
    }else{
    return(32+pos);
    }
    }
    }

    std::string ASCII_Code_to_character(ASCII_Code code){
    if(code==10){
    return(std::string("\n"));
    }else if((32<=code)and(code<=126)){
    return(std::string(printableCharactersInASCII[code-32]));
    }else{
    throw std::exception("Not an ASCII printable character");
    }
    }

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 28, 2009
    #6
  7. Jerry Coffin Guest

    In article <cf0a3865-7f74-4d61-ab70-
    >, says...
    > This is a very dumb question... I have a std::vector<std::string> that
    > I'm pushing ascii charcters into like so:
    >
    > set.push_back("a");
    > set.push_back("b");
    > ...
    > ...
    >
    > I'm doing this manually. This is error prone and time consuming. Does
    > something in the STL have these already? I'm rather new to C++ so
    > forgive me if this is blatantly obvious. Google did not help much. The
    > goal is to get all typable ascii chars into the vector.


    My first question would be exactly what you're trying to accomplish with
    this. You mention ASCII, but that's a fairly obsolete encoding -- you're
    much more likely to encounter something like ISO 8859 nowadays. Are you
    sure you really need ASCII per se, or do you want whatever character set
    is being used on the target computer?

    Assuming what you really want is the information for the current locale
    (or at least some known locale) you can retrieve it from that locale
    instead of trying to generate it on your own. For the global locale, it
    can look like this:

    for (int i=0; i<CHAR_MAX; i++)
    if (isprint(i))
    set.push_back(i);

    For another locale, things get a bit uglier, but not horribly so:

    std::locale loc("");

    for (int i=0; i<CHAR_MAX; i++)
    if (std::use_facet<ctype<char> >(loc).isprint(i))
    set.push_back(i);

    For the moment, I've assumed a char-based encoding -- if you want a
    wchar_t based encoding instead, things could get a little hairy. For
    UCS-4 encoded ISO 10646, your string would occupy something like 16
    gigabytes (32-bit encoding, so each character is 32 bits, and you have 2
    ^32-1 of them possible, and a large number of those are printable. For
    such a situation, I'd think hard about fiding some fundamentally
    different method of doing what you need (possibly using the locale's
    functions instead of storing their results).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Apr 30, 2009
    #7
    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. TOXiC
    Replies:
    5
    Views:
    1,287
    TOXiC
    Jan 31, 2007
  2. Laszlo Nagy
    Replies:
    6
    Views:
    640
  3. Terry Reedy
    Replies:
    0
    Views:
    525
    Terry Reedy
    Jul 1, 2008
  4. M.-A. Lemburg
    Replies:
    0
    Views:
    904
    M.-A. Lemburg
    Jul 2, 2008
  5. Alextophi
    Replies:
    8
    Views:
    546
    Alan J. Flavell
    Dec 30, 2005
Loading...

Share This Page