Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All

I want to get bytes of a string in an integer format. please any one help me!

Thanks for your answers; I want the size of a string in integer. any one clearly answer my question please.
Posted
Updated 6-Jan-11 22:16pm
v2

Hi,

you can use this method:
C#
private byte[] StringToByteArray(string str)
{
    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    return enc.GetBytes(str);
}

and in e.g. a button click event you can do:
C#
byte[] arr = StringToByteArray("Hello");

Please also see Hiren Solanki's answer for more information.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Jan-11 0:58am    
Formally, this is correct answer. However, I am not 100% sure what was Abdul's question. Let's see...
Hiren solanki 7-Jan-11 1:21am    
Good half answer JF. Don't worry I've taken care of you afterwords.
Dalek Dave 7-Jan-11 3:47am    
Good call!
Follow the answer provided by JF2015 for getting byte array of string.

Further more HERE[^] is a good article on Lovely MSDN for caring of you further.
 
Share this answer
 
v2
Comments
JF2015 7-Jan-11 1:23am    
Thanks for this additional information. By the way, I noticed that the link you posted is msdn in spanish - you could try posting the link with "en-us" instead of "es-es". Have my 5
Hiren solanki 7-Jan-11 1:27am    
Thanks JF for notifying I've changed it accordingly to en-US.
Dalek Dave 7-Jan-11 3:47am    
That'll Do It!
Do you mean get the Length[^] of a string?
 
Share this answer
 
Comments
JF2015 7-Jan-11 4:32am    
Yes, according to the obscure question, I think this is the correct answer. 5!
thatraja 8-Jan-11 2:09am    
right answer
Use Params.[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jan-11 1:00am    
Sorry, Kasson, this time this is quite irrelevant to the question (even though the question is not 100% clear to me, but surely params are irrelevant).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900