"std::vector<unsigned char>" - what is it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bobrick

    "std::vector<unsigned char>" - what is it?

    Hi.

    I'm in the process of making a GUI for a function someone else wrote,
    and i've come across a type i'm unfamiliar with, namely
    "std::vector<un signed char>". I need to get the contents of this
    variable into a form I can display in a text box, but i'm not sure what
    to expect inside of the variable, whether I can just treat it like an
    array e.t.c. Any help would be appreciated.

    Thanks,

    Bob

  • Peter Jansson

    #2
    Re: &quot;std::vect or&lt;unsigned char&gt;&quot; - what is it?

    Bobrick wrote:
    Hi.
    >
    I'm in the process of making a GUI for a function someone else wrote,
    and i've come across a type i'm unfamiliar with, namely
    "std::vector<un signed char>". I need to get the contents of this
    variable into a form I can display in a text box, but i'm not sure what
    to expect inside of the variable, whether I can just treat it like an
    array e.t.c. Any help would be appreciated.
    >
    Thanks,
    >
    Bob
    >
    It is a vector of unsigned char's. You can treat is as an array as follows:

    std::vector<uns igned charvar;
    const std::vector<uns igned char>::size_typ e N(var.size());
    // Write it to cout and to a string:
    std::ostringstr eam s;
    for(int i=0;i<N;i++)
    {
    // Notice the [i]
    std::cout<<var[i]<<'\n';
    s<<var[i];
    }
    std::cout<<s.st r()<<'\n';


    Sincerely,

    Peter Jansson


    Comment

    • Colander

      #3
      Re: &quot;std::vect or&lt;unsigned char&gt;&quot; - what is it?

      Hi There,
      I'm in the process of making a GUI for a function someone else wrote,
      and i've come across a type i'm unfamiliar with, namely
      "std::vector<un signed char>". I need to get the contents of this
      variable into a form I can display in a text box, but i'm not sure what
      to expect inside of the variable, whether I can just treat it like an
      array e.t.c. Any help would be appreciated.
      If you have the time, have a look at the following page:


      If you have less time, look at:


      Greets,
      Bas

      Comment

      • Alf P. Steinbach

        #4
        Re: &quot;std::vect or&lt;unsigned char&gt;&quot; - what is it?

        * Peter Jansson:
        Bobrick wrote:
        >Hi.
        >>
        >I'm in the process of making a GUI for a function someone else wrote,
        >and i've come across a type i'm unfamiliar with, namely
        >"std::vector<u nsigned char>". I need to get the contents of this
        >variable into a form I can display in a text box, but i'm not sure what
        >to expect inside of the variable, whether I can just treat it like an
        >array e.t.c. Any help would be appreciated.
        >>
        >Thanks,
        >>
        >Bob
        >>
        >
        It is a vector of unsigned char's. You can treat is as an array as follows:
        >
        std::vector<uns igned charvar;
        const std::vector<uns igned char>::size_typ e N(var.size());
        // Write it to cout and to a string:
        std::ostringstr eam s;
        for(int i=0;i<N;i++)
        {
        // Notice the [i]
        std::cout<<var[i]<<'\n';
        s<<var[i];
        }
        std::cout<<s.st r()<<'\n';
        It's also possible that the vector is meant to directly represent a
        string. In which case the simplest way to convert it to the string
        format required by the OP's text box depends on that string format.
        Converting to a std::string by copying (inefficient but works):

        std::string s( v.begin(), v.end() );

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Jim Langston

          #5
          Re: &quot;std::vect or&lt;unsigned char&gt;&quot; - what is it?

          "Bobrick" <pukill57@gmail .comwrote in message
          news:1168269816 .707472.180060@ 51g2000cwl.goog legroups.com...
          Hi.
          >
          I'm in the process of making a GUI for a function someone else wrote,
          and i've come across a type i'm unfamiliar with, namely
          "std::vector<un signed char>". I need to get the contents of this
          variable into a form I can display in a text box, but i'm not sure what
          to expect inside of the variable, whether I can just treat it like an
          array e.t.c. Any help would be appreciated.
          >
          Thanks,
          >
          Bob
          The vector itself you can pretty much treat as an array. The contents of
          the unsigned chars will be values from 0 to 255. What those represent, I
          don't know. Are they characters? Are they values? You'll have to look at
          the way it's used to determine that.

          The fact that it's unsigned char instead of plain char would lead me to
          suspect it's used for it's values. Many ways to iterate through a vector.
          The one that most C programs would understand would be:

          // presume Array is std::vector<uns igned char>
          for ( size_t i = 0; i < Array.size(); ++i )
          std::cout << Array[i] << "\n";

          Or you can iterate using iterators:
          for ( std::vector<uns igned char>::iterator it = Array.begin(); it !=
          Array.end(); ++it )
          std::cout << *it << "\n";

          In addition, an arrays memory is guaranteed to be contiguous.

          unsigned char* vecp = &(Array[0]); // grab address of first unsigned char
          for ( size_t i = 0; i < Array.size(); ++i )
          std::cout << *(vecp + i) << "\n";


          Comment

          Working...