473,406 Members | 2,710 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,406 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 39243
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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,...

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.