472,980 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 39147
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

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

Similar topics

1
by: kim | last post by:
Scenario (I'm a newbie): I have a datagrid with countries listed and 5 parameters in each row. I want to add a row to this datagrid via an Event Handler. Very basic stuff. This method then call a...
6
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
32
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
0
by: =?Utf-8?B?RGF2aWQ=?= | last post by:
I need to convert an incoming byte array to a Unicode string, when (if) that byte array contains valid Unicode values (sometimes it doesn't). I've been trying to use...
1
by: Sergei Shelukhin | last post by:
Hi. I have a string, 16 bytes long, representing a hex number. I need to convert it to a number and put it into pgsql database; it complicates the matters further cause pgsql doesn't support...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.