473,396 Members | 1,921 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.

String --> Byte array

How can string be converted into a byte array in C++ ?
Can I used std::bitset<> to do it ?
Jul 19 '05 #1
4 48815

"qazmlp" <qa********@rediffmail.com> wrote in message news:db**************************@posting.google.c om...
How can string be converted into a byte array in C++ ?
Can I used std::bitset<> to do it ?


Yoiu can just call data() to get a transient const char* that points
to the beginning of a character array. Alternatively, you can copy
it to a vector:

string s = ...

vector<char> v(s.size());
copy(s.begin(), s.end(), v.begin());
Jul 19 '05 #2

qazmlp <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
How can string be converted into a byte array in C++ ?
Can I used std::bitset<> to do it ?


std::string s("Hello");
char *array = new char[s.size()];
std::copy(s.begin(), s.end(), array, array + s.size());
/* etc */
delete [] array;

-Mike

Jul 19 '05 #3
qazmlp wrote:
How can string be converted into a byte array in C++ ?
Can I used std::bitset<> to do it ?


A string IS an array of 'string::value_type' underneath.

Why do you need to convert it ?

Here is an example - you can use the method
"data()" and return a const char *

Or, why not just use iterators as arrays ? (second example).

#include <iostream>
#include <string>
int main()
{
std::string foo( "ABC" );

//examp 1
const char * array = foo.data();

array[0] + array[2];

//examp 2
std::string::iterator iter_array = foo.begin();

iter_array[ 2 ] = 'x';

std::cout << foo << '\n';

}
Jul 19 '05 #4

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...
|
| "qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
| > How can string be converted into a byte array in C++ ?
| > Can I used std::bitset<> to do it ?
|
| Yoiu can just call data() to get a transient const char* that points
| to the beginning of a character array. Alternatively, you can copy
| it to a vector:
|
| string s = ...
|
| vector<char> v(s.size());
| copy(s.begin(), s.end(), v.begin());

Alternatively:

vector<char> v( s.begin(), s.end() );

Cheers.
Chris Val
Jul 19 '05 #5

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

Similar topics

4
by: Magix | last post by:
Hi, I would like to convert a string to byte array. I'm using Win32 Visual C++. Thanks.
17
by: cpptutor2000 | last post by:
Could some C guru help me please? A byte is an unsigned char in C. How do I convert from a C string to a corresponding byte array. Any help would be greatly appreciated.
11
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
5
by: Tom | last post by:
Stupid Question: Have a string ("ABC"); need to convert that to a byte array (Dim x(3) as Byte). Do I have to do this manually or is there a .NET command to do it automatically? Tom
8
by: tbh | last post by:
for historical reasons i need to be able to call, from C# under DotNet 2, as COM+ DLL function that returns a "string" which is really an array of seemingly arbitrary bytes (presumably non-zero)....
2
yabansu
by: yabansu | last post by:
Hi all, This is my first message! Using C++ standard libraries, I want to convert a string to a byte array and a byte array to the string. How can I do that? I did it in C# .NET as the...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
3
by: eran otzar | last post by:
ok i want to take a byte array byte buffer = ASCIIEncoding.ASCII.GetBytes("hello there"); now yes we turned it into a byte, but now i want to write the represent to contents of the byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
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
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,...
0
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...

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.