|
arsing strings differs greatly depending on the strongs you have to parse.
If you know the format will always be the same, i.e. "." marks the beginning and again the end of the value you want to retrieve, you can IndexOf() to get these positions and then use them to calcualte the substring that can contsin a vaule.
Then use int.Tryparse (all number types have TryParse methods) to convert the value to a number.
Thhis methods returns a bool that tells you if the parsing was successful or not.
|
|
|
|
|
Megidolaon wrote: arsing strings differs greatly depending on the strongs you have to parse
Is your spell-checker turned off?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
My brain was turned off.
|
|
|
|
|
If the format of the string is always the same, and there are only ever going to be two '.'s in it then use String.Split .
Off the top of my head something like:
string[] parts = myinputstring.Split(".".ToCharArray());
string numericBit = parts[1];
Hope this helps.
But as I have done this from memory, PLEASE look up the Split Method first.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: Split(".".ToCharArray()
Your memory is exaggerating the method's complexity. The method accepts a char[] but since the parameter is defined with the params keyword a simple Split('.') would suffice.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Yeah, well errr.... I'm very old you know, why I can remmember when.... ZZZzzzzzzzzzzzzz.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: I'm very old you know
repeating this all you will achieve is convincing yourself.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I would like to start off by saying that I have never done this before, so please excuse if this is a silly mistake.
I have a function within a DLL that I need to call. The C# project will fail on occasion with an exception, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have been looking around, and my best guess is using strings in my project will cause errors.
Any help from the experts?
Here's the code I'm using in C#
public class Encryption
{
[DllImport("Encryption.dll", EntryPoint = "encrypt")]
public unsafe static extern IntPtr encrypt(char [] data, char [] key);
static public string Encrypt(string data, string key)
{
if (data.Length > 0)
{
return Marshal.PtrToStringAnsi(encrypt(data.ToCharArray(), key.ToCharArray()));
}
else
return "";
}
}
And the function in the DLL (C++)
extern "C"
{
__declspec(dllexport) string encrypt(char* data, char* key)
{
...
}
}
Thanks,
-Kalivos
|
|
|
|
|
kalivos wrote: __declspec(dllexport) string encrypt(char* data, char* key)
Hello. What's that string thingy? Did you mean std::string ? If so, I don't think it's possible to import it since encrypt function is not returning a POD. My guess is that you should either change std::string to a char* or use C++/CLI instead.
As said, it's only my guess. Perhaps others could aid you better.
Stupidity is an International Association - Enrique Jardiel Poncela
|
|
|
|
|
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
//string attach .attachment file name
System.Web.Mail.MailAttachment attached = new System.Web.Mail.MailAttachment(attach,System.Web.Mail.MailEncoding.Base64);
mm.Attachments.Add(attached);
problem is that attachments.add method do not accept object of MailAttachment.
..............................
if i use
System.Net.Mail.Attachment attached = new Attachment(attach);
mm.Attachments.Add(attached);
this works fine
/////////////////////////////////
I want to convert attachment to base64 encoding then how will i do if i use
System.Net.Mail.Attachment
|
|
|
|
|
Hi,
How can i sort an int[]?
I need to ge the greatest value from int[].
Thankyou
YPKI
|
|
|
|
|
Using the latest version of the .NET framework,
var ints = new[] { 5, 15, 1, 7 };
var orderedInts = ints.OrderByDescending(i => i);
foreach (var value in orderedInts)
{
Console.WriteLine(value);
}
This will print out 15, 7, 5, 1.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
ypki wrote: How can i sort an int[]?
Array.Sort[^] can do that.
|
|
|
|
|
I believe that will sort the integers from least to greatest, opposite of what he's looking for.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Judah Himango wrote: from least to greatest,
which Array.Reverse could fix; and I wouldn't be surprised when the Sort+Reverse combination would prove to be faster than all the new and fancy stuff...
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
If it's faster, it's likely a miniscule difference. Also consider that OrderByDescending is delay executed, and since this guy needs only the first element, it's probably faster in this case than doing separate array operations that touch all the elements twice.
But since we're nitpicking, I'd say that
var greatest = ints.OrderByDescending(i => i).First();
is clearer than
Array.Sort(ints);
Array.Reverse(ints);
int greatest = ints[0];
Intent is clearer, and allows for more advanced abstractions, such as the forthcoming PLINQ.
Of course, all this is probably zero relevance for this guy asking the question, since he just wants the greatest element in an array. That we're nitpicking is the only reason I bring it up!
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
There is no need to reverse anything if all required is the largest value, just sort and pick the last value; or don't sort and just traverse once keeping track of the largest so far, as in:
int max=int.MinValue;
foreach(int i in ints) if(max<i) max=i;
assuming the compiler is smart enough to enumerate the array without actually creating an enumerator, this should work without any method call and be the fastest way to get it done.
If not, use good old for and an index...
As for readability the only one really acceptable is int max=ints.Largest; which AFAIK is not yet supported; extension methods is one thing, how about extension properties?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
(I'm amused by this thread, as we've so wildly veered off helping this guy and are now deeply engaged in an irrelevant, pseudo-friendly programming pissing match! )
Is the compiler smart enough to convert that into a for loop? Otherwise we have the overhead of allocating an iterator, making a manual for loop being the fastest (or at least, requiring the fewest allocations).
Of course, we're trading readability for fewer allocations in the hopes of better performance, trading our nice declarative code for very detailed imperative code that's hard to optimize, since we're being so specific about how we want the results, rather than declaring the results we want.
In this era of 4 and 8 core desktop machines, with 32 cores coming in the next 5 years, perhaps the fastest and most readable will be:
var greatest = ints.AsParallel().OrderByDescending(i => i).First();
Oh hell yes.
Your move.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Judah Himango wrote: we've so wildly veered off helping this guy
Didn't he get ample help for his quest? Can't help it he only needed a little bit of help, the matter deserves deeper examination.
Judah Himango wrote: era of 4 and 8 core desktop machines
no matter what amount of hardware resources you throw at the problem at hand, performing a full sort when none is required cannot possibly be the winning strategy. If you want to parallelize (new word?) the quest for the largest number, just split the data set over the cores, and pick the largest of all the largests. BTW: that is of course what int max=ints.Largest; is supposed to do.
Anyway, 2 to 4 threads is fine for my needs; most of the additional ones we will have in future will be very busy performing utilitarian stuff such as:
- aggregating the latest MSDN help info;
- RSS feeding different web sites, trying to keep up with all the CP forums and the like;
- scanning for viruses;
- automatically reporting WPF bugs;
- downloading and installing fixes for Visual Studio and the like (excluding WPF, that one is unfixable from the start according to JSOP).
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: BTW: that is of course what int max=ints.Largest; is supposed to do.
Ah. Ok then, I give up. When will you be writing an article that introduces extension properties added to C# 3 and the new array.Largest extension property?
Luc Pattyn wrote: Anyway, 2 to 4 threads is fine for my needs; most of the additional ones we will have in future will be very busy performing utilitarian stuff such as:
Indeed...
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Judah Himango wrote: When will you be writing an article
When the new stuff becomes available and only if MSDN fails to document it properly.
I'll try and come up with some other article topics in the mean time...
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Judah Himango wrote: opposite of what he's looking for.
Well, not really. Since he wants the greatest, he just needs to know whether to get it from array[0] or array[array.Length(-1?)], once it is sorted.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
More than one way to skin a cat.
Likewise, you could use
var greatest = ints.OrderBy(i => i).Last();
I'd argue the LINQ way is more declarative, the intent is clearer, and allows for more advanced abstractions. Whatever the case, though, it probably doesn't matter to the guy asking the question.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
modified on Thursday, April 2, 2009 12:08 PM
|
|
|
|
|
Judah Himango wrote: More than one way to skin a cat
First you have to catch it.
Good job he's using .Net.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
How to create table using rectangle in pictureBox1???
Thanks before...
Iman Ridwan
|
|
|
|