468,244 Members | 2,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,244 developers. It's quick & easy.

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

yabansu
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
6 38083
willakawill
1,646 1GB
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
horace1
1,510 Expert 1GB
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
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
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
horace1
1,510 Expert 1GB
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
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.

Similar topics

8 posts views Thread by Grant Richard | last post: by
13 posts views Thread by Jack MacRank | last post: by
1 post views Thread by Sergei Shelukhin | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.