|
I am so nice I wrote a utility method for you:
static public void ProcessFile(string inPath, string outPath)
{
using (StreamReader r = File.OpenText(inPath))
using (FileStream f = File.Create(outPath))
using (StreamWriter w = new StreamWriter(f, Encoding.ASCII))
while (!r.EndOfStream)
w.WriteLine(HttpUtility.HtmlEncode(r.ReadLine().ToUpper()));
}
This assumes it's alright to process the file line by line; I did it because it saves a few lines of code. If your file doesn't contain many lines you should consider reading blocks of characters instead - this ensures that the methods memory footprint stays the same regardless of the file fed to it. It also makes for more efficient memory allocation since you can then reuse the same allocated character array for each read (whereas readline allocates a new string each time).
HTH
|
|
|
|
|
I've created a wrapvalue function in C#, but I can't figure out how to make it work with a lower bound, like a negative.
Here's the function at the moment:
private int WrapValue( int val, int upperBound ) {
return val - ( upperBound * ( val / upperBound ) );
}
WrapValue( 1023, 360 ) would return 303.
By the way ( val / upperBound ) is integer division.
It works great for positive values, but not negatives and you can't specify a lower bound. Does anyone know how I to make it work with negatives and a lower bound so I could do something like this:
WrapValue( int val, int lowerBound, int upperBound )
WrapValue( -300, -500, 123 )
Thanks in advance.
By the way I'm using this to scroll a semi-transparent image over another image in the shape of a circle to create the illusion of a rotating sphere. The semi-transparent image needs to wrap around in a certain way and this function will help me accomplish that if I can get working correctly.
modified on Sunday, July 20, 2008 5:19 PM
|
|
|
|
|
Hi,
what you are trying to achieve is to alias an original value into a range [low,high);
the way to achieve that is by adding/subtracting high-low as often as is required
to get the value inside the given range.
We know that (for integers and with b>0) a-a/b*b returns a number in [0, b)
that is the range including 0 and up to but excluding b.
And the modulo operator does exactly the same: a%b gives the remainder of a/b
So it will look like
newValue=origValue-(origValue-low)%(high-low)
[ADDED] above formula is wrong, both test cases don't detect it.
It should be: newValue=low+(origValue-low)%(high-low)
[/ADDED]
Fast proof (using diff for high-low):
1. for origValue=low, newValue=origValue-0*diff which equals low
2. for origValue=high, newValue=origValue-1*diff which equals high-(high-low)
hence low as it should
You'll have to check the behavior for negative numbers, I expect it to be all right.
modified on Sunday, July 20, 2008 6:01 PM
|
|
|
|
|
hmmm...
When I try this function:
private int WrapValue( int val, int lowerBound, int upperBound ) {
return val - ( val - lowerBound ) % ( upperBound - lowerBound );
}
with:
WrapValue( 1023, 0, 360 ) I get 720 as a result.
when I try this function:
private int WrapValue( int val, int lowerBound, int upperBound ) {
return val - ( val - lowerBound ) / ( upperBound - lowerBound );
}
with:
WrapValue( 1023, 0, 360 ) I get 1021 as a result.
But, shouldn't I be getting 303 still?
I haven't tried with negatives or a different lower bound yet.
|
|
|
|
|
Hi,
sorry my mistake, the formula should be:
newValue=low+(origValue-low)%(high-low)
which really means wrap around every high-low, but perform an offset of low
In your second snippet, you had / again instead of %
|
|
|
|
|
Well it's closer...
Using this function:
private int WrapValue( int val, int lowerBound, int upperBound ) {
return lowerBound + ( val - lowerBound ) % ( upperBound - lowerBound );
}
WrapValue( 1023, 0, 360 ) gives me 303.
But WrapValue( -5, 0, 360 ) gives me -5, but it shouldn't it give me 355?
|
|
|
|
|
OK,
This page[^] explains a bit about modulo for negative numbers.
Seems like in C# a%b will return in (-b,0] for negative a, always assuming positive b.
To fix this, you will need a conditional test.
Either check for origValue<low and="" use="" another="" formula="" if="" it="" mode="hold">or keep the formula, and add another diff if it resulted below low.
As a hack, you could forget the test, and start of with originalValue+99999*(high-low)
but doing so reduces your useful range and/or risks an overflow, which would ruin
everything.
|
|
|
|
|
So I could check to see whether originalValue is negative or not and if it is I do what?
|
|
|
|
|
if the result is less than low, adding another high-low
seems the easiest way of dealing with it.
|
|
|
|
|
Nice, it works
The function:
private int WrapValue( int val, int lowerBound, int upperBound ) {
int result = lowerBound + ( val - lowerBound ) % ( upperBound - lowerBound );
if( result < lowerBound ) {
result += ( upperBound - lowerBound );
}
return result;
}
I tried it with all different bounds and values and it works great.
Thanks a ton
|
|
|
|
|
|
sadiyaVasanthKumar wrote: Postfix Multidigit Expression Evation in C#
You left out the 'lua' - does that help?
What was the question??
Maybe lua[^] is the answer?
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Actually he is missing an "alu"[^].
|
|
|
|
|
Agreed, since there was no logic in the OP
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I can't seem to figure this out...
I have a value within the range of 0 to 40 and I want to be able to convert that into a value in the range of 0 to 10,000.
This is for a simple music player that I have built into one of my programs. I'm using C# and DirectX.AudioVideoPlayback to play mp3 files. I have a working custom slider control that has a range from 0 to 40. This will be used to adjust the volume of an mp3.
The problem is that volume in DirectX.AudioVideoPlayback ranges from 0 to 10,000 (technically -10,000 to 0... 0 being max volume). Therefore I need to figure out a formula to convert a value from my custom slider control that is in the range 0 to 40, to a value DirectX.AudioVideoPlayback can use which is in the range of 0 to 10,000. Also these values are integers and the conversion doesn't have to be exact, just close enough.
I'm bad at math, so any ideas?
|
|
|
|
|
did you consider multiplying by 250?
|
|
|
|
|
Nice, it works. So 10000 / 40 = 250... I knew it was something simple.
Thanks a ton
|
|
|
|
|
You're welcome.
|
|
|
|
|
To that things you can always use the Rule of 3 (literal traduction from "Regla de 3" on spanish, don't know the correct name)
RangeMax1 ----- RangeMax2
Value1 -------- Value2
To know the equivalent of one value into another different range. Lets say you have the RangeMax1, RangeMax2 and Value1 and you want to know the Value2. It is made with:
Value2 = (RangeMax2 * Value1) / RangeMax1
if you want to find Value1 then:
Value1 = (RangeMax1 * Value2) / RangeMax2
You multiply the 2 members of the fully known diagonal, and divide with the other number in the diagonal of the desired value.
If you use the RangeMaxes and the Unit... you have the generic formule of your transformation or the coeficient you need.
40 ------ 10000
1 ------- x
x = 10000 * 1 / 40; x = 250
EDIT:
I forgot to say, that the range has to be in absolute width. I mean [-2, 18], translates into 0 ---- 20, you should afterwards adapt the calculated value.
Let's say... I have the value -1,5 in the range [-5, 10] and want to know its equivalent in the range [5, 42]
10 - (-5) = 15
42 - 5 = 37
-1,5 - (-5) = 3,5
15 ------ 37
3,5 ----- x
x = (37 * 3,5) / 15 = 8,633
8,633 + 5 = 13,633
the equivalent is 13,633
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Hi,
I've for a similar problem
I have a Volume Control that only has values from 0 to 100 and i want to replace a slider control that ranges from -12 to 12??? any help please!
J.Feeke
|
|
|
|
|
The multiplication by 250 suggestion is the easiest, but the human ear response isn't linear; it's logarithmic. A range of 0 to 40 dB corresponds to a range of 0 to 10,000 mW power level exactly, so I suggest that a conversion implementing the equation:
DirectX.AudioVideoPlayback.Volume = 10^(MySlider.Volume/10)
might be more useful.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
I totally forgot that it was Audio... you are totally right, the conversion must consider the change between dB and electrical units. I would give you a 5 if I was not in the firm. (I'll do it when I'm home)
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Since the allowed range for the volume (according to OP) is -10,000 to 0 , maybe the effect is already accounted by the DirectX function.
I.e. maybe the function accepts sound level instead of volume.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
The Threading.Interlocked.CompareExchange operation is wonderful, from both a theoretical and practical perspective. It allows for lock-free implementations of many algorithms. For example, to add something to a stack:
Sub AddToStack(X as Whatever)
Dim OldStack as Whatever
Do
OldStack = TheStack.Next
X.Next = OldStack
Loop While Threading.Interlocked.CompareExchange(TheStack.Next, X, OldStack) <> OldStack
End Sub Nice and simple and elegant. If no other thread messes with the stack during the execution of the above code, it runs once. If another thread messes with the stack, it may have to run again, but if will never have to wait for another thread to do something. It may have to wait for another thread to not do something, but since the only obstructive actions will be those that achieve useful progress, there is no danger of livelock.
A couple of wrinkles in the CompareExchange ointment, though:- Some data structures require more than one instruction to 'commit' changes; implementations of such data structures will be more complicated.
- Many algorithms require that if the object being 'CompareExchange'd is changed, the CompareExchange detect it. If new objects are created and used every time something changes, the CompareExchange will detect it and there will be no problem. If objects are reused from a pool, however, it's possible that an object someone was expecting to CompareExchange will be disposed of (returned to the pool), reallocated, and put in the place of the old object that was being 'CompareExchange'd.
What is the best way to take advantage of CompareExchange while avoiding dangers like object reuse? Physically, I would think that since the Intel processors allow a CompareExchange of a 64-bit long, they should be able support a CompareExchange of a data item that consists of an object plus a 32-bit 'reuse' count; even on 64-bit platforms, a 64-bit data type should be able to hold an object plus a 'reuse' count (I can't imagine any need for more than 2^32 objects to be handled by one thread in the foreseeable future). Unfortunately, .Net doesn't support any such constructs and I know of no way they could be simulated without boxing and unboxing (which would defeat the whole purpose).
How would one best implement something like the following:
Class AtomicPair
Public Class Pair
Public Thing1,Thing2 as Whatever
End Class
Public MyPair as Pair
Public Sub SetPair(ByVal NewThing1 as Whatever, NewThing2 as Whatever)
Dim NewPair as New AtomicPair.Pair
NewPair.Thing1 = NewThing1
NewPair.Thing2 = NewThing2
MyPair = NewPair
End Sub
Public Sub SetThing1(ByVal NewThing1)
Dim NewPair as New AtomicPair.Pair
Dim OldPair as AtomicPair.Pair
NewPair.Thing1 = NewThing1
Do
OldPair = MyPair
NewPair.Thing2 = OldPair.Thing2
While Threading.Interlocked.CompareExchange(MyPair, NewPair, OldPair) <> OldPair
End Sub
End Class Assume a similar method for SetThing2. As implemeted using 'New', the above will work reliably to allow atomic updates of one or both fields of the pair. If objects were allocated from a pool instead of using New, however, the code could break. What are the best approaches to dealing with the issue?
|
|
|
|
|
Hi, would like to ask u a Java question.
Example i have a array of Books called bookArray
Inside the bookArray i store the following :
bookArray[0] = { 1001, "JAVA Book 1", 1 }
bookArray[1] = { 1001, "JAVA Book 1", 2 }
bookArray[2] = { 1001, "JAVA Book 1", 3 }
bookArray[3] = { 1001, "JAVA Book 1", 4 }
bookArray[4] = { 1002, "C# Book 1", 1 }
bookArray[5] = { 1003, "F# Book 1", 1 }
How do i traverse the array to read from bottom up? meaning when i traverse the array, the first result i get is
bookArray[5] = { 1003, "F# Book 1", 1 }
|
|
|
|
|