473,791 Members | 3,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert string to byte array and vice versa?

yabansu
14 New Member
Hi all,

This is my first message!

Using C++ standard libraries, I want to convert a string to a byte array and a byte array to the string. How can I do that?

I did it in C# .NET as the following:

Expand|Select|Wrap|Line Numbers
  1. string msg = "Hello!";
  2. byte[] buffer = new byte[msg.Length];
  3.  
  4. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  5. buffer = encoding.GetBytes(msg);
What is the C++ way of doing the same thing as .NET GetBytes method did?

Thanks at all,
yabansu
Dec 25 '06 #1
2 24059
horace1
1,510 Recognized Expert Top Contributor
you can use the c_str() function in <string> to get a const pointer to an array of char, e.g.
Expand|Select|Wrap|Line Numbers
  1.     const char * ch=s.c_str();
  2.     cout << ch << endl;
  3.  
see http://www.cppreferenc e.com/cppstring/index.html
Dec 25 '06 #2
asdfghjkl2007
30 New Member
string s="0a"; ////string that should be converted to a bit_array
unsigned short int byte_array[100];
unsigned short int b[8];// used for storring the binary form of x=(int)s[i];
int x;

//converting a string to an array
int i=0;
int h=0;
while(s[i])
{
x=(int)s[i]; // converts a char to its int form
for(int j=0;j<8;++j)
{ // b[] stors the binary form of x but backwords
b[j]=x%2;
x=x/2;
}

for(int j=7;j>-1;--j)
{
byte_array[h]=b[j]; //adds b to byte_array[] but in the right way
++h;
}

++i;
}


////convert to string
// i supouse the array contoins elements
int j;
i=0;
cout<<" byte_array lenght = ?";
cin>>i; // you have to know the number of elements

string st=""; // empty string
if((i%8)==1) cout<<"error the array can't be converted to a string because the number of bits ins't a 8*K number";
// a char is represented by 8 bits so to be able to convet byte_array to a array of char ( a strig ) the number of
// elements of byte_array must be a multipal of 8 ( 8*K number k is an unsignedint)
else
{
h=0;
j=0;
x=0;
while(h<i)
{
x=x*2+byte_arra y[h]; // converting a grup af 8 bits to a int wich reprezent the code of a char
++h;
++j;
if(j==8)
{
j=0;
st=st+(char)x; //storing it in the string
x=0;
}
}
}
Dec 25 '06 #3

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

Similar topics

13
7924
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
1
16994
by: War Eagle | last post by:
Is it possible to convert an unsigned long to byte array. For instance, unsigned long = 23480923 unsigned long longNumber; longNumber=23480923; // in binary = 1 0110 0110 0100 1010 0101 1011 // so i want byteArray=0000 0001 , byteArray=0110 0110 , // byteArray=0100 1010, byteArray=0101 1011. Is there a way to get the 4 bytes that make up this variable? Also, is there a way to take a 4 byte array and convert it into a long?
3
5702
by: Rodusa | last post by:
I am looking for an example of how to save a DataTable into Base64 and vice-versa. I tried using Convert.ToBase64String(), but it says that it cannot convert DataTable to byte. Any help or pointing would be appreciated. Thanks Rod
3
11471
by: simonc | last post by:
Can you define a property as type Byte array of a specific length? I am trying to pass a byte array which is 3200 bytes in length from one form (in which the bytes are read from a file) to another form (where the content can be edited). I have defined a property for the editing form using the following Dim m_edicin(3199) As Byte Public Property ebcdicin() As Byte Get
1
6507
by: Vitaly Zayko | last post by:
How to convert bit string to byte and vice versa? For example: string bitstr = "11111111"; after conversion should get 0xFF -- Vit Zayko
1
2534
by: Eugene Anthony | last post by:
Private Function BStr2UStr(BStr) 'Byte string to Unicode string conversion Dim lngLoop BStr2UStr = "" For lngLoop = 1 to LenB(BStr) BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr,lngLoop,1))) Next End Function Private Function UStr2Bstr(UStr)
1
19202
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
6
39290
yabansu
by: yabansu | last post by:
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? ...
0
10785
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9669
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.