473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert std::string to std::vector<unsigned char> and vice versa

Hi group,

how to convert a string to a vector of unsigned char ?
I used to iterate trough the string to set the vector, but I think
this is not the best way to do this.
I'm a beginner with the STL

How about allocating the vector ? should I know before the size ?

And I would like to do then opposite conversion, from an unsigned char
vector to a string.
How to do this ? how about special caracters ? (\n for example)

Thanks for your help
Best regards,

N.

Apr 2 '07 #1
3 40510
* ti*********@gmail.com:
>
how to convert a string to a vector of unsigned char ?
std::string s = "abra kadabra";
std::vector<unsigned charv( s.begin(), s.end() );

I used to iterate trough the string to set the vector, but I think
this is not the best way to do this.
I'm a beginner with the STL

How about allocating the vector ? should I know before the size ?
Huh.

And I would like to do then opposite conversion, from an unsigned char
vector to a string.
How to do this ?
std::string t( v.begin(), v.end() );

how about special caracters ? (\n for example)
Irrelevant.

--
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?
Apr 2 '07 #2
On 2 Apr, 14:20, timor.su...@gmail.com wrote:
Hi group,

how to convert a string to a vector of unsigned char ?
I used to iterate trough the string to set the vector, but I think
this is not the best way to do this.
I'm a beginner with the STL

How about allocating the vector ? should I know before the size ?
If you have very large strings you can run v.reserve(s.size()) before
inserting, which might give a speed improvement. But you don't have
to.

--
Erik Wikström

Apr 2 '07 #3
On 2 avr, 15:37, "Erik Wikström" <eri...@student.chalmers.sewrote:
On 2 Apr, 14:20, timor.su...@gmail.com wrote:
Hi group,
how to convert a string to a vector of unsigned char ?
I used to iterate trough the string to set the vector, but I think
this is not the best way to do this.
I'm a beginner with the STL
How about allocating the vector ? should I know before the size ?

If you have very large strings you can run v.reserve(s.size()) before
inserting, which might give a speed improvement. But you don't have
to.

--
Erik Wikström

Thanks both for your answer, that solves my problem :)

Best regards,

N.

Apr 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
4
by: Bobrick | last post by:
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<unsigned char>". I need to get the contents of this...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Andrew Wingorodov | last post by:
i have std::vector<stringarg; for dynamic program configuration. i need get const char* argv; for execv(3). how i can make its simple? there may be ready solutions? -- www.andr.ca
12
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
1
by: hanna88 | last post by:
what's the best way to convert a list of string to a vector<int>? what i have now is for loop and iterate each char in the string string base=""; //i have vector<int>row,vector <...
3
by: Michel Caillat | last post by:
Here is a small C++ puzzling problem which blocks me in my current developments and I will appreciate any idea or explanation. Consider the following C++ program : #include <stdint.h>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.