Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:54:50+00:00 2026-06-10T22:54:50+00:00

I’m reading multiple reports from a HID device into an unsigned char , then

  • 0

I’m reading multiple reports from a HID device into an unsigned char, then trying to copy the data to a std::vector. I’m also writing the data out to a file for hex analysis, whose content appears to be correct when I view it. However, the std::vector doesn’t appear to contain the correct data when I dump it to the console.

This is the code:

typedef vector<unsigned char> buffer_t;

buffer_t sendCommand (hid_device *devh, const unsigned char cmd[], int reports) {
    unsigned char outbuf[0x40];
    buffer_t retbuf(0x40 * reports);

    hid_write(devh, cmd, 0x41);

    int i;
    FILE *file = fopen("test.out", "w+b");
    while (i++ < reports) {
       hid_read(devh, outbuf, 0x40);
       fwrite(outbuf, 1, sizeof(outbuf), file);
       retbuf.push_back(*outbuf);
    }
    fclose(file);
    cout << &retbuf[0];
    return retbuf;
}

I have a feeling I’m way off the mark here; I’m fairly new to C/C++, and I’ve been stuck with this for a while now. Can anyone tell me what I’m doing wrong, or point me in a better direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:54:51+00:00Added an answer on June 10, 2026 at 10:54 pm

    You want to add multiple unsigned char objects to your vector, but push_back only adds one.

    So, replace retbuf.push_back(*outbuf); with either:

    for (size_t i = 0; i < sizeof(outbuf); ++i) {
        retbuf.push_back(outbuf[i]);
    }
    

    or

    std::copy(outbuf, outbuf+sizeof(outbuf), std::back_inserter(retbuf));
    

    or

    retbuf.insert(retbuf.end(), outbuf, outbuf+sizeof(outbuf));
    

    which all do the same thing.

    You create your vector with a certain size:

    buffer_t retbuf(0x40 * reports);
    

    but push_back increases the size of the vector by adding an element at the end. You should create it empty:

    buffer_t retbuf;
    

    Optionally, you could arrange for the vector to have enough space allocated, ready for the elements you’re going to add:

    retbuf.reserve(0x40 * reports);
    

    This is purely a performance issue, but sometimes it’s a significant issue for large vectors, or vectors of types that (unlike unsigned char) are expensive to copy/move when the vector runs out of internal space and has to allocate more.

    A note on style: you repeat the literal value 0x40 a few times, and also use sizeof(outbuf). It’s often best to define a constant, and use the name throughout:

    const int report_size = 0x40;
    

    This is partly in case the number changes in future, but also it’s about the readability of your code — if someone sees 0x40 they may or may not immediately understand why that is the correct value. If someone sees report_size then they don’t know what value that actually is until they look it up, but they do know why you’re using that value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.