unsigned short array to string

Discussion in 'C++' started by greyham, Feb 11, 2005.

  1. greyham

    greyham Guest

    This is a beginner question but is there a better way to do this? I
    have an array of unsigned shorts and need to convert it into a string
    array with the numbers separated by spaces. I've started to make an
    intermediate string of characters that represents the digits. Am I on
    the right track? I know java so there must be a simple way in C++ that
    I don't know about since I'm new to the C++ world. Thanks for the
    help!

    int i=0;
    int j=0;
    const int DataLengthPerChannel=4;
    const int maxDigits = 8;
    unsigned short dataOut[DataLengthPerChannel];
    short digit[maxDigits];

    //initialize data
    for (i=0; i<DataLengthPerChannel; i++) {
    dataOut=200*i+74;
    }
    //first loop around every element of dataOut
    //then loop around the digits of the numbers
    for (j=0; j<DataLengthPerChannel; j++) {
    for (i=maxDigits-1; i>=0; i--) {
    digit = dataOut[j]%10;
    dataOut[j] /= 10;
    }//for
    }//for
    greyham, Feb 11, 2005
    #1
    1. Advertising

  2. greyham wrote:
    > This is a beginner question but is there a better way to do this? I
    > have an array of unsigned shorts and need to convert it into a string
    > array with the numbers separated by spaces.


    You need to get your spec straight. It's either a string array (where
    each element is a representation of the respective element of the other
    array) or it's a single string with numbers separated by spaces.

    Take a look at 'ostringstream' class. You can simply output all your
    shorts into one array and then get the string out of it.

    > [...]


    V
    Victor Bazarov, Feb 11, 2005
    #2
    1. Advertising

  3. greyham

    Jerry Coffin Guest

    greyham wrote:
    > This is a beginner question but is there a better way to do this? I
    > have an array of unsigned shorts and need to convert it into a string
    > array with the numbers separated by spaces. I've started to make an
    > intermediate string of characters that represents the digits. Am I
    > on the right track? I know java so there must be a simple way in C++
    > that I don't know about since I'm new to the C++ world. Thanks for
    > the help!


    I'm not entirely sure I understand what you want, but if my guess is
    correct, you're looking for something like this:

    std::vector<short> dataout;
    std::eek:stringstream inter;

    // create the data
    for (int i=0; i<DataLengthPerChannel; ++i)
    dataout.push_back(200*i+74);

    // format the data
    std::copy(dataout.begin(), dataout.end(),
    std::eek:stream_iterarator<short>(inter, " "));

    // get the formatted output as a string.
    std::string concatenated_output = inter.str();

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 11, 2005
    #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. Replies:
    4
    Views:
    816
    Kaz Kylheku
    Oct 17, 2006
  2. fancyerii
    Replies:
    21
    Views:
    1,472
    Roedy Green
    Nov 5, 2007
  3. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    672
    Eric Sosman
    Mar 5, 2008
  4. Gabriel Genellina
    Replies:
    0
    Views:
    513
    Gabriel Genellina
    Aug 13, 2009
  5. pozz
    Replies:
    12
    Views:
    733
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page