|
string str = "This is a test";
byte[] byteArray = new byte[str.Length];
for (int i=0;i < str.Length;i++)
byteArray[i] = (byte) str[i]; This has to be the most asked question in this forum. It just doesn't seem quite worth writing an article for, does it?
Paul
|
|
|
|
|
as far as i remember string is unicode. thus each character consists of 2 bytes. your code may work for ascii-characters, but the real solution would rather be something like:
string str = "This is a test";
byte[] byteArray = new byte[<font color=red>2*</font>str.Length];
for (int i=0;i < str.Length;i++)
{
<font color=red>int c = (int)</font>str[i];
byteArray[<font color=red>2*</font>i] = (byte) c;
<font color=red>byteArray[2*i+1] = (byte)(c>>8);</font>
}
or something similar...haven' tried the above code...
looking at the system.text-namespace is the best i believe...
:wq
|
|
|
|
|
Interesting point. I suppose it depends what you want the byte array for. If it's to store in a database then you might want to compress it.
Incidentally, your code wouldn't work either (at least I don't think so). str[i] returns a char so the high-byte of your int would always be zero.
This is fine in most cases but you might as well say
byteArray[2*i] = (byte) str[i];
byteArray[2*i+1] = 0; However all this is fairly acedemic since it was pointed out that System.Text.Encoding.ToByteArray.GetBytes(str) seems to do the job perfectly.
Paul
|
|
|
|
|
Paul Riley wrote:
str[i] returns a char so the high-byte of your int would always be zero.
char is unicode friendly, so the high-byte will depend on whether the text is ascii or unicode.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
It is? Gawd-damn! Amazing the things you can go around not knowing, isn't it?
Apologies to anyone I've misinformed on this one.
Paul
|
|
|
|
|
Look at System.Text.Encoding
|
|
|
|
|
Eric Gunnerson (msft) wrote:
Look at System.Text.Encoding
Well... that's a bit cool, ain't it?
Paul
|
|
|
|
|
Thank you
modified 29-Aug-18 21:01pm.
|
|
|
|
|
I think it's something like:
ASCIIEncoding ae = new ASCIIEncoding();
byte[] byStr = ae.GetBytes(str);
-- LuisR
──────────────
Luis Alonso Ramos
Chihuahua, Mexico
www.luisalonsoramos.com
"Do not worry about your difficulties in mathematics, I assure you that mine are greater." -- Albert Einstein
|
|
|
|
|
hi
it seems there is a new step accomplished on the road of generic implementation: it's called Gyro
see @
http://research.microsoft.com/projects/clrgen/
it looks cool
what do you thinks about it?
NB: it seems this guy deliver upgrade every 4 monthes since last year and this it beta .. perhabs a proposal at the end of the year??
|
|
|
|
|
Hi,
I would like to read/write a specific format to a flat text file. This format will contain delimiters like |, or space. I have been looking at the String class in C# to find somthing like string tokenizer. No any luck. Can someone help me out? Thanks!
|
|
|
|
|
string str = "this|is|a|test";
string[] arr = str.Split('|');
string str = "this|is,a|test";
string[] arr = str.Split('|', ',');
HTH
Paul
|
|
|
|
|
String.Split Method
String Class | String Members | System Namespace
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.
Overload List
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.
public string[] Split(params char[]);
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array. A parameter specifies the maximum number of array elements to return.
public string[] Split(char[], int);
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
RegExp
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
hi..
i have a problem with my listbox,
it doesn't allow me to display turkish characters like "ü, ı, etc,.."
my editbox displays whatever i type in it but my listbox not!...
although i've changed listbox font settings (script=turkish, or font=Arial TR)
nothing changed...
All my religional settings works fine. i have no problem with displaying except from my listbox.
What should i do?
any help will be appreciated...
Thanks ,
Atilla Selem
just listening to you...
|
|
|
|
|
hi,
this is more a question on working with vs.net;
back in the c++-vs6-days there was an easy way to get a list of all virtual (or base-class) functions and have an overwrite-corpus in your code with one click.
i wouldn't know about things like WndProc or IsInputKey if i wouldn't have this forum here. is there any way in vs.net to easily get a list of the available virtual functions and say "i want to overwrite this" with one mouse-click?
thx.
:wq
|
|
|
|
|
Hi!
If you higlight a class then there is a button on the properties window that lets you override base class virtual functions with one click!
Cheers
HTH
Martin
"Situation normal - all fu***d up"
Illuminatus!
|
|
|
|
|
i didn't find that button, but i found something else. if you walk the class-view tree into the base classes, there's an option "add-override" to each virtual methods contextmenu.
thx for your help.
:wq
|
|
|
|
|
O I didn't know that one! Thanks! But at least with c++ there's a button at the top of the properties window (when displayed while a class is highlited in class view) labelled ovveride (I have the german version of VS .NET) or similiar. It's next to the button where you create message overrides.
Cheers
Martin
"Situation normal - all fu***d up"
Illuminatus!
|
|
|
|
|
I have a computer with below minimum specifications for VS .NET (450MHz/128RAM + XP Pro). An upgrade is planned at the beginning of the next year.
Do you think I could run VS .NET somehow on this machine? (All I plan to do is to learn the .NET Framework)
Any tips are appreciated, thank you!
|
|
|
|
|
Visual Studio.NET requirements:
450Mhz
64Mb RAMM
No 9x system
and bla bla... is that enough?
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
I run a PII 300 with VS.NET and it's fine.
Granted, it's got 192MB of memory, but I'm sure 64MB would be OK on a non-server and just running the framework.
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
So the VS.NET don't say "You don't have enough with mhz"?
But you need a WinNT system! I tried ínstalled it on a WinME computer...
It didn't allowed me to do that!
But WinME sucks anyway!
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
what version of windows are you running
|
|
|
|
|
Make sure you have lot of time to waste.
May the Source be with you
Sonork ID 100.9997 sijinjoseph
|
|
|
|