473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert std::string to std::vector<uns igned 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 40671
* ti*********@gma il.com:
>
how to convert a string to a vector of unsigned char ?
std::string s = "abra kadabra";
std::vector<uns igned 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...@gma il.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.siz e()) 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.sewro te:
On 2 Apr, 14:20, timor.su...@gma il.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.siz e()) 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
4872
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
6819
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> buf(SIZE); fread(&buf, 1, SIZE, f);
4
20088
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 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,
6
5646
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. char temp; std::vector<unsigned charmmessage; while (!done){
6
5713
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
4336
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
5680
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
4397
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 < vector<int>row > mat; //what i want to do with the string is that everytime char != ";" row.pushback(str that convert to int)
3
9386
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> #include <vector> using namespace std; int main(int argC, char* argV) {
0
8821
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.