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

  • Home
  • SEARCH
  • 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 9155951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:41:10+00:00 2026-06-17T12:41:10+00:00

I receive data as a vector<char> , from which I need to create a

  • 0

I receive data as a vector<char>, from which I need to create a string. Vector may contain utf-16 characters (i.e. null bytes) and is a fixed size. Actual data is padded with null bytes to this fixed sized. So, for example, I can have the following vector:

\0 a \0 b \0 c \0 d \0 \0 \0 \0

Fixed size is 12 and the vector contains utf-16 string “abcd” padded with 4 null chars to size.

From this, I need to actually extract this string. I already have the code for converting from utf-16 to string, the thing where I got myself confused is find the number of characters (bytes) in the vector without the padding. In the example above, the number is 8.

I started by doing something like:

std::string CrmxFile::StringFromBytes(std::vector<char> data, int fixedsize) {

    std::vector<char>iterator it = data.rbegin();
    while(it != data.rend() && *it == '\0') {
        it++;
    }

    return std::string(&data[0], fixedsize - (it - data.rbegin());
}

However in the full context, the vector contains a lot of data and I need to do the above manipulation with only a specified part of it. For example, the vector may contain 1000 elements and I need to get the string that starts at position 30 and goes for a max of 12 chars. Of course, I can create another vector and copy the required 21 characters into it before applying the above logic, but I feel that I should be able to do something directly on the given vector. Yet, I can’t grasp what iterators I am comparing with what. Any help is appreciated.

  • 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-17T12:41:11+00:00Added an answer on June 17, 2026 at 12:41 pm

    Now, this is embarrassing: vector<char>::iterator is obviously a random access iterator, therefore I can decrement it. Hence my method now looks like this:

    std::string CrmxFile::StringFromBytes(std::vector<char> data, int fixedsize) {
        std::vector<char>::iterator begin = data.begin() + start;
        std::vector<char>::iterator end = start + length - 1;
        while(it >= begin  && *it == '\0') {
            it--;
        }
    
        if(it >= begin) {
            int len = it - begin + 1;
            if(IsUtf8Heuristic(begin, begin + len) {
                return std::string(begin, begin + len);
            }
            else {  //(heuristically this is utf-16)
                len = ((len + 1) >> 1) << 1;
                std::string res;
                ConvertUtf16To8(begin, begin + len, std::back_inserter(res));
                return res;
            }
        }
        else {
            return "";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my iOS application. I receive binary vector data from
i need to send and receive data to/from a microcontroller using java using usb
I'm currently developing an Android application which needs to receive data from a bluetooth
I am using vector<char> to send and receive data via socket. In this vector
I need to receive data packets send from a GSM modem through GPRS with
i am developing an iphone app that receive data from sql server through php
I have an application that may receive data via various methods and in various
I have dynamic listview on my android client app that receive data from remote
I'd like to use the NIO to send/receive data to/from a distant machine. I
Good day, I receive data from a communication channel and display it. Parallel, I

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.