By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

How to convert "string" into "byte array" and vice versa?

yabansu
P: 14
Hi all,

I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte[]), to convert string into byte array and vice versa.

Now, I want to do the same thing in pure(unmanaged) C++.

I searched the Internet but could not find any satisfactory solutions. I am really in need of help! Is there anyone to explain how I can implement these functions by not using .NET library?

Thanks a lot...
Jan 30 '07 #1
Share this Question
Share on Google+
6 Replies


100+
P: 1,646
Hi. In c++, BYTE is an unsigned char and a string is a char array. Could you let us know where you want to start and where you want to end and why?
Jan 30 '07 #2

Expert 100+
P: 1,510
if you are using C++ strings the c_str() function will return a const pointer to an array of char where each char is a byte?
http://www.cppreference.com/cppstring/index.html
Jan 30 '07 #3

yabansu
P: 14
Hi. In c++, BYTE is an unsigned char and a string is a char array. Could you let us know where you want to start and where you want to end and why?
Hi willakawill,
I am working on a basic client/server application. Here, it is supposed to encrypt some data and send the encrypted data to each other in xml packets.
The encryption algorithm I used, takes byte array as argument and gives byte array as output. Since the resultant byte array cannot be directly inserted into the xml packet, I need to convert it into a string. Also, the other side needs to convert it into the byte array to use after parsing the xml packet.
I hope the problem is clear.
Jan 30 '07 #4

yabansu
P: 14
To make more clear, I need the following two functions:

1. a function takes a string parameter and returns the unsigned char array representation of the string
2. a function takes a unsigned char array parameter and returns the string representation of the array
Jan 30 '07 #5

Expert 100+
P: 1,510
To make more clear, I need the following two functions:

1. a function takes a string parameter and returns the unsigned char array representation of the string
2. a function takes a unsigned char array parameter and returns the string representation of the array
if you usethe C++ string class
http://www.cppreference.com/cppstring/index.html
the c_str() function will do (1) and there is a constructor to do 2.
Jan 31 '07 #6

P: 1
If you're using std::string, that won't be necessary, because a C++ string is already a byte array. Use string.length() to get the number of bytes, and string.data() to get a pointer to the first byte. But it really depends on what you plan to do with those bytes.
Jan 14 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.