How to convert a String to a byte array? [Edit]

Hi,


I want to send a string (UnicodeString typed variable) over the serial port. I have to convert the string to a byte array, which I can write to the serial port. The project options _TCHAR maps to wchar_t. Thus, how to convert a String to a byte array in C++ Builder 2010?

On Microsoft Developer Network I have found: 

Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

You can investigate it your self here: http://msdn.microsoft.com/en-us/library/system.text.encoding.convert%28VS.71%29.aspx

Please do not hesitate to answer or comment this issue. All help is  appreciated.


Regards 
Jan Rouvillain
0
Jan
2/5/2010 8:42:52 AM
embarcadero.cppbuilder.cpp 2803 articles. 0 followers. Follow

4 Replies
2997 Views

Similar Articles

[PageSpeed] 57
Get it on Google Play
Get it on Apple App Store

Jan Rouvillain <> wrote:

>I want to send a string (UnicodeString typed variable) over the serial port.

 WriteFile( port, unicodeString.c_str(), 2 * unicodeString.Length(), &byteswritten );

>I have to converet the string to a byte array, which I can write to the serial port.

Why?

...
0
Bob
2/4/2010 8:42:59 PM
> {quote:title=Bob Gonder wrote:}{quote}
> 
> >I have to converet the string to a byte array, which I can write to the serial port.
> 
> Why?
> 
> ..
I have as many others used AnsiString's mutabilty to send commands to equipment attached to the serial port and read responses as well. The equipment works with bytes and 8 bit characters, the char c-type. Another point is, that I have been used to use AnsiString as a mutable type for a sequence of bytes and chars. This behaviour of AnsiString seems changes in C++ Builder 2010, because VCL is 100% unicode string based.

I miss the easy conversion to char and byte in the C++ Builder 2012 UnicodeString. The String has to hold the original type, it was created with. Thus, if it is initialised with chars, then it holds a buffer of char, which an be accessed using ToChar(). I achieve this functionality setting up the project option "_TCHAR maps to char" and use t_str() to convert. Will C?? Builder support the combination t_str() and project option "_TCHAR maps to char"?
0
Jan
2/5/2010 12:12:03 PM
Hello,

Jan Rouvillain wrote:

> This behaviour of AnsiString seems changes in C++ Builder 2010,
> because VCL is 100% unicode string based.

note that AnsiString is still there, without changes. You can use it as
you did before.


> I miss the easy conversion to char and byte in the C++ Builder 2012
> UnicodeString. The String has to hold the original type, it was
> created with. Thus, if it is initialised with chars, then it holds a
> buffer of char, which an be accessed using ToChar(). I achieve this
> functionality setting up the project option "_TCHAR maps to char" and
> use t_str() to convert.

Don't /ever/ use t_str(). It changes the underlying data
representation, which may lead to obscure errors in other code which
expects the string to hold UTF-16 data.

Also, according to Allen Bauer
(http://blogs.embarcadero.com/abauer/2010/01/26/38908) the string
checks which make t_str() possible are likely to go away in future
C++Builder versions, which effectively means that t_str() will vanish
as well.

Use AnsiString (someUnicodeString).c_str () instead. If you need to
change the actual content of a UnicodeString, use a local AnsiString
copy and assign it back and forth.

-- 
Moritz

"Hey, it compiles! Ship it!"
0
Moritz
2/5/2010 12:16:06 PM
"Moritz Beutel" <grue@zork.com> wrote in message 
news:210894@forums.codegear.com...

> Also, according to Allen Bauer
> (http://blogs.embarcadero.com/abauer/2010/01/26/38908) the
> string checks which make t_str() possible are likely to go away
> in future C++Builder versions, which effectively means that
> t_str() will vanish as well.

That is true.  t_str() should never have been introduced in the first place, 
and will be corrected in the next version.

-- 
Remy Lebeau (TeamB)
0
Remy
2/6/2010 5:59:57 AM
Reply:

Similar Artilces:

How to append string to Response.OutputStream after string converted to byte array
Hi, I am trying to append string to Response.OutputStream property in PostRequestHandlerExecute event handler inside custom httpmodule. The goal of this implementation is to append string to output stream after the page is rendered when the response object is still held on the server. The pseudo code I wrote is the following: private void PostRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = ( HttpApplication ) sender; HttpContext context = application.Context;   string s = "This is test!"; byte[] buffer = Encoding.ASCII.GetByt...

String to Linq.Byte and Linq.Byte to String convertion?
 Hi, I have a field in my database as image type. I am using Linq to read/insert records.I am having little hard time inserting text(string) into the image field and then converting back to string.Do you have any suggestions? Thanks in advance,   The below example shows how to convert Linq Binary to byte If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("imageID")) Then Dim imageID = HttpContext.Current.Request.QueryString("imageID") Dim db As New AuctionsDataContext Dim result = From img In db.Image...

How do I convert string to a byte array?
[see also http://forums.sybase.com/cgi-bin/webnews.cgi?cmd=item-232695&group=sybase.public.powerbuilder.general] Working with an old OLE object for TCP comm, and the unicode vs ANSI charset change at PB 10 seems to be causing issues. BUT! the old object avoids "'wide' to ANSI" conversion and just sends what it's handed if it's handed a BYTE Array. However, getting there from a string seems to involve conversion to and parsing from a Blob, something that I'm fumbling. Some help on this conversion process would be appreciated. Convert the string to ...

Problems converting a byte array to string
Hello,i need some help on this one, i have a byte array with some information i get from an UDP serverhere is an example of what my code looks like Dim b() As Byte = {0, 179, 68, 252, 255, 128, 234, 244} Dim s As String = System.Text.Encoding.Default.GetString(b) Debug.WriteLine(s) Now if notice the first byte is 0, when this happens my result string will be nothingAny ideas on how to fix this?Thank you   I notice you're using encoding Default... 0 is often a string terminator, which could be your issue. Maybe try some of the other encodings until you find one that fi...

convert byte[] to String and string to byte[] note encoding and decoding required....
hi all,        As the subject, can I have solution ofr the same.......  e'i Convert byte[] into string and strting into byte[] , can i have teh sample page.        Thanks & Regards,Murthy.          System.Text.Encoding.Unicode.GetString(ByteArray);//Byte Array to String             System.Text.Encoding.Unicode.GetBytes(String);//String to Byte ArrayWe All move to our original place.Don't forget.Imran Baloch thanks for...

Convert string to string array.
Hi, We have a string array argument in datawindow object for 'IN' sql statement. Our user enter a string in a single line edit. How to convert the string to a string array for the argument of datawindow? ex. user input : 'A', 'B', 'C' => str_arr[]={'A', 'B', 'C'} dw_1.retrieve(str_arr) Thanks in advance. Take a look at the n_cst_string object in the pfc. It has a method called of_ParseToArray that does exactly what you're looking for. It's a pretty simple task to 'port' the code ...

value of string cannot be converted to 1 dimensional array of byte
Im still going with a small app im doing and i have the byte data currently in a string array and i would then like ot use that in the below line but the underlined bit gives the error:  value of string cannot be converted to 1 dimensional array of byte Response.BinaryWrite(CByte(ImageString))     my full code is:   Private Sub OpenText() Dim filename As String = "c:\images.txt" Dim ObjStreamReader As StreamReader Dim CurrentString As String Dim PrevString As String Dim icount As Integer Dim St...

how to convert array of ansichar to array to byte?
Hi, I have a very hard time to convert array of ansichar to array to byte. I tried {code} arr: array[0..32767] of AnsiChar; bArr: TByteArray; move(arr, bArr, numBytesToRead); {code} and it doesn't work. they are array of 8 bit and I wonder what is the correct way to do this. Is there a way to cast this as well? I have search all over the internet and couldn't find any information about this. Could anybody help me? thanks alot, edwin chang wrote: > Hi, I have a very hard time to convert array of ansichar to array to byte. > I tried > > {code} ...

How to convert object to Byte[] (Byte array)
Hello how are all here? I need to convert an object into byte[] (byte array). The line of code is below: byte[] DocImage = new byte[] DocImage = returnData["DocImage"] (which returns an object) Now How can I convert object to byte[]? There does not exist any built-in conversion from object to byte[] like Convert.ToInt32, Convert.ToString etc Thanks byte[] DocImage = new byte[] DocImage = (byte[] )returnData["DocImage"] Regards,Anas Ghanem.Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like dup...

RSA Encryption
Here is my goal:1. Take a string2. Encrypt it 3. Pass it as a parameter in the QueryString4. Decrypt it The value starts as a string, then is converted to a byte[] and then encrypted.  The resulting byte[] is converted to a string and send as a parameter.  The recieving page decrypts the string (creates a byte[], decrypts to a new byte[], and the value is finally parsed for its values) I am using http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrsacryptoserviceproviderclasstopic.asp with a small modification t...

Safe to convert int array into BYTE array?
It it safe to convert a *size_t* into a *BYTE* like I'm doing here? It seems to work fine. {code} size_t HELLO_WORLD[] = {0x48,0x65,0x6C,0x6C,0x6F,0x20,0x57,0x6F,0x72,0x6C,0x64}; size_t iSize = sizeof(HELLO_WORLD) / sizeof(size_t); BYTE * pArray = (BYTE *)malloc(iSize); for(size_t i = 0; i < iSize; i++) { pArray[i] = BYTE(HELLO_WORLD[i]); } {code} <Jason Goff> wrote in message news:400034@forums.embarcadero.com... > It it safe to convert a *size_t* into a *BYTE* like I'm doing here? It > seems to work fine. Yes, as long as none of the elemen...

string to byte array
Hi all, I'm currently building a small app which uses 3DES encryption, it works all nicely, but would like to do a bit a fix upcurrently the code behind has the code as followDim key As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22.2, 23, 24}and the web.config is as follows    <appSettings>    <add key="key" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22.2, 23, 24"/>    </appSettings> I am trying to call the values {1, 2 ,...

superreview granted: [Bug 226597] nsObjectFrame.cpp Warning: String literal converted to char* in initialization. : [Attachment 136193] use properly sized arrays
Robert O'Callahan <roc@ocallahan.org> has granted timeless@myrealbox.com <timeless@bemail.org>'s request for superreview: Bug 226597: nsObjectFrame.cpp Warning: String literal converted to char* in initialization. http://bugzilla.mozilla.org/show_bug.cgi?id=226597 Attachment 136193: use properly sized arrays http://bugzilla.mozilla.org/attachment.cgi?id=136193&action=edit ...

superreview requested: [Bug 226597] nsObjectFrame.cpp Warning: String literal converted to char* in initialization. : [Attachment 136193] use properly sized arrays
timeless@myrealbox.com <timeless@bemail.org> has asked Robert O'Callahan <roc@ocallahan.org> for superreview: Bug 226597: nsObjectFrame.cpp Warning: String literal converted to char* in initialization. http://bugzilla.mozilla.org/show_bug.cgi?id=226597 Attachment 136193: use properly sized arrays http://bugzilla.mozilla.org/attachment.cgi?id=136193&action=edit ...

Web resources about - How to convert a String to a byte array? [Edit] - embarcadero.cppbuilder.cpp

List of converts to Buddhism - Wikipedia, the free encyclopedia
Ajahn Brahm (born 1951), Religious teacher and student of the late Thai monk Ajahn Chah . Currently he is the abbot of the Bodhinyana Monastery, ...

Facebook Timeline For Pages: 8 Million Converts, 29 Million To Go
... reach to 98 percent of users who liked its page, as well as to double its engagement rate, during a 28-day period. Dr. Pepper hasn’t yet converted ...

Facebook stock roundup: Execs offload more stock, directors convert shares
... Bowles, the former co-chair of the now-defunct National Commission on Fiscal Responsibility and Reform, and Reed Hastings, Netflix CEO, converted ...

How to Convert Leads Into Buying Customers - The 9 Step Sales Process EVERYONE Should Use
... School of Business in Toronto on the topics of Sales and Entrepreneurship. I shared with them the sales process that I find How to Convert Leads ...

Bitcoin : Should I convert my savings to Bitcoin?
Answer (1 of 7): You should think of this question as "Should I invest all my savings in Bitcoin?", and not "Should I have my savings denominated ...

Coordinates - Calculate and Convert a Position on the App Store on iTunes
Get Coordinates - Calculate and Convert a Position on the App Store. See screenshots and ratings, and read customer reviews.

Minority Ahmadiyah Muslim group in Indonesia told: convert or be expelled
A minority Muslim group has been ordered to convert to Sunni Islam or be expelled from Bangka island, off the coast of Sumatra, in the latest ...

Convert your images quickly and easily with PDF Photos
Have you ever wanted an easy way to convert images to a PDF? Maybe you want to include them in a document for work, send them via email at a ...

Ford patents rear wheel that converts into a unicycle
Filed under: Government/Legal , Green , Ford , Green Driving , Motorcycle , Electric Ford is thinking about allowing drivers to remove a rear ...

How to convert your old VHS tapes to DVD with this software
Remember those old VHS tapes you have sitting in the corner? Convert them into DVDs with this affordable software so you can easily watch them ...

Resources last updated: 2/4/2016 12:26:18 AM