473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

yabansu
14 New Member
Hi all,

I think most of you probably know the two .NET framework functions, namely Encoding.GetByt es(string) and Encoding.GetStr ing(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 39255
willakawill
1,646 Top Contributor
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 Recognized Expert Top Contributor
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.cppreferenc e.com/cppstring/index.html
Jan 30 '07 #3
yabansu
14 New Member
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 New Member
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 Recognized Expert Top Contributor
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.cppreferenc e.com/cppstring/index.html
the c_str() function will do (1) and there is a constructor to do 2.
Jan 31 '07 #6
Daggerbot
1 New Member
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
10756
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 Business method, which calls a Data method which calls an SP in MS SQL. My code compiles well, but the page throws "Index was outside the bounds...
6
2821
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 uneasy leaving it hanging in memory indefinitely (especially in case when string is Interned). So at leats for the case when string is not...
32
2658
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
8
21902
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 that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This...
13
28241
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 command methods. This is exactly what the Microsoft Data Access Application Block assembly does but I coded my own simple, custom class. I have a...
23
2074
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 becomes a different operation than with regular strings because the circular string can be "rotated", like this: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1...
0
1294
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 UnicodeEncoding.GetString(bytes)... However, when the byte array doesn't contain value Unicode values, I get a "Found a low surrogate char without a preceding high surrogate...
1
5377
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 unsigned; thus I have to subtract "magic" number (half the bigint range) from the conversion result to put it into pgsql. Is there any way to achieve...
9
4083
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 puts() to print to screen ????
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.