|
Sorry - this is not working.
|
|
|
|
|
This won't work, at least not in the way you want it... You are writing a numeric algorithm which should work upon different 'arithmetic' types and want to utilize arithmetic operators on T , right? This is not possible. There is no common base type for all 'numerics' which implements the appropriate operators (operators must be defined static on the type which implements them). Each CLR numeric type is implemented as a structure without common base (which is not supported for structures anyway), ergo you cannot restrict a type parameter in a generic to something which does not exists. The numeric types implement various interfaces, but these cannot implement operators since these must be static . So you are stuck, for the moment.
The only clean way (without CLR sourcery) is to define this non-existent common base:
public abstract class NumericBase<T> where T : struct {
public static NumericBase<T> operator + (NumericBase<T> pA, NumericBase<T> pB) {
return pA.Add(pB);
}
public static NumericBase<T> operator - (NumericBase<T> pA, NumericBase<T> pB) {
return pA.Sub(pB);
}
public abstract T Value { get; set; }
public abstract NumericBase<T> Add(NumericBase<T> pOther);
public abstract NumericBase<T> Sub(NumericBase<T> pOther);
}
Specialize for int :
public class NumericInt : NumericBase<int> {
public NumericInt() : this(0) {}
public NumericInt(int pValue) {
Value = pValue;
}
public override int Value { get; set; }
public override NumericBase<int> Add(NumericBase<int> pOther) {
return new NumericInt(Value + pOther.Value);
}
public override NumericBase<int> Sub(NumericBase<int> pOther) {
return new NumericInt(Value - pOther.Value);
}
}
And you can 'Foo ':
public class MySummator<T>
where T : struct {
public NumericBase<T> Result { get; set; }
public void Foo(NumericBase<T> pParameter) {
Result = Result + pParameter;
}
Clearly, you have a lot of overhead with virtual calls, so I would not use it that way in situations where performance is critical. On the other hand, you could use a bit of CLR sourcery here:
1. Write your algorithms using NumericBase<T> . You can run and test them and you are maintaing one generic common code base.
2. For real world usage System.Emit code, which is structurally identical to the generic NumericBase implementation, but uses concrete numeric types i.e. you replace NumericBase<int> with int and so on.
|
|
|
|
|
hi all,
normally in win app. or web app. I can take windows log on username with this code
"System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();"
but with windows service I can't do this, it takes like this:
"NT AUTHORITY\SYSTEM"
Howcan I do this with windows service??
thanks..
|
|
|
|
|
What account have you defined it to run under? Seems like Local System..
|
|
|
|
|
Go to your control panel-Admin Tools-Services and right click on the service, select properties -Log On tab ... have fun.
BTW,
System.Security.Principal.WindowsIdentity.GetCurrent().Name
is a string. If you call ToString on a string you look foolish. It is my personal crusade to get .NET developers to understand the concept of type.
|
|
|
|
|
Dumb answer of the day.
The service is running on a SERVER, the user name is what you started the service with. You nee dto get the username from the CLIENT and pass it to the service to record with the data.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi.
I now know how to ask my question I have been trying to find out, without confusing people.
All I would like to do is take a string of digits
"123456789"
and get its byte representation (hex) value.
The above, 123456789 entered into the calculator gives :
75BCD15
How would I do that in c#
There, finaly I think I have put what I am trying (desperatly) to do. sorry for all the posts and proberbly need to say sorry for being so confusing.
Thank you in advance
Steve
|
|
|
|
|
Parse it as an int (or long if you expect it to be that big)
Then do something like string.Format("{0:X}", value) or value.ToString("X")
|
|
|
|
|
Thank you.
Yes, that works, but the string will be 41 digits long and I do not what to use bignum or anything.
I am trying to get this function to work, it did in c++, I have modified it but it produces the wrong hex value and I do not know why.
static int decodeSerial(String serial, byte[] result, int ResLen)
{
int m;
int digit;
Array.Clear(result, 0, ResLen);
for (int i = 0; i < serial.Length; i++)
{
digit = serial[i] - '0';
m = 0;
do
{
digit += result[m] * 10;
result[m++] = (byte)(digit % 255);
digit >>= 8;
}
while (m < ResLen);
}
return 0;
}
Thank you
Steve
|
|
|
|
|
Here is an implementation, together with a series of tests.
I'm not claiming it is anywhere near the optimum, I mimicked your code for as much as I liked it.
Given full freedom I would return a byte array of variable length and never have to signal an overflow.
private static void runAllTests() {
test("1");
test("1000");
test("1000000");
test("1000000000");
long j=1;
for(long i=0; i<63; i++) {
test(j.ToString());
j+=j;
}
test("-1");
test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
}
private static void test(string decimalDigits) {
try {
byte[] bytes=decodeSerial(decimalDigits, 10);
string s=decimalDigits+" = ";
foreach (byte b in bytes) s+=" "+((int)b).ToString("X2");
log(s);
} catch (Exception exc) {
log(exc.ToString());
}
}
private static byte[] decodeSerial(string decimalDigits, int numberOfBytes) {
byte[] bytes=new byte[numberOfBytes];
foreach (char c in decimalDigits) {
int digit="0123456789".IndexOf(c);
if (digit<0) throw new ArgumentException("bad char in decodeSerial("+decimalDigits+")");
for (int m=0; m< numberOfBytes; m++) {
digit+=bytes[m]*10;
bytes[m]=(byte)(digit&0xFF);
digit>>=8;
}
if (digit!=0) throw new OverflowException("decodeSerial("+decimalDigits+","+numberOfBytes+")");
}
return bytes;
}
FYI: log(string s) is any method that somehow outputs a string, could be as simple as Console.WriteLine(s);
|
|
|
|
|
Thank you very much.
Obviosly I take it you now understand what I meant originaly?
It was hard to explain lol.
thank you again,
Steve
|
|
|
|
|
stephen.darling wrote: It was hard to explain
Not really, however you made it rather hard to understand. I summarized it in a three-line comment (I just added an example).
|
|
|
|
|
Yes you did
Now I just need to figure out a function that will do the reverse, take the byte array and product the original string!
Do I dare ask for more help?
Steve
|
|
|
|
|
Yes you dare, and no you're not going to get a solution from me.
As far as I can tell, you haven't learned a single thing yet, all I saw you do was copy some C++ code,
then massage it blindly until it compiled well and ran all wrong. It is time to learn and understand what is really going on.
If you want more help, you write code yourself, debug it, and when stuck, post here, and we all will help you.
|
|
|
|
|
That is fair enough.
I am trying, and I have managed to learn some things from my books. I have managed to write my first little game that works well.
I dont what you to write the code for me, just point me in the right direction.
When it comes to this area it is not covered in my books, and because I dont know what search terms to use for this kind of programing (You see how hard it was to explain what I wanted to do) I find it difficult to find out.
I will try and plod on and lear, hopefully I didnt offend, or worse still come across as a lazy person not willing to learn on my own.
Also, if I may add, I tried my best to rewrite the code so that I didnt have to ask anyone in the first place, and I have been on with this problem for over 2 months before asking for help.
Thank you again for your time.
Steve
|
|
|
|
|
You're welcome.
Seems like we will meet again.
|
|
|
|
|
stephen.darling wrote: I have been on with this problem for over 2 months before asking for help
I admire your ambition, but maybe it's time for you to tackle simpler problems for the time being (before you burn out on problems beyond your capability). I saw nothing in your posts that indicated you were taking a logical approach to solving this problem. You didn't post that you had tried debugging it incrementally by outputting values as the algorithm ran. You didn't post that you tried with very simple values and made them more and more complex until you could reproduce the bug. You didn't describe the mathematical reasoning behind the algorithm (if you don't know how it's supposed to work, how are you supposed to make it work). You couldn't even describe what it is you were so desperately trying to do for the past 2 months (if you can't define the question, you can't solve the problem). Seriously, take a step back and take another look at the basics. Finding a mentor would be good; I mean a real, live person... not some random people on a forum. A teacher, a friend who is good at programming, or even a tutor at your school (if you are going to school) would be great. It would be a shame to waste all the passion you seem to have on reaching too high too quickly and experiencing only failure. You need to experience success if you are going to keep doing programming. Good luck.
|
|
|
|
|
Is that the new beginner assignment?
|
|
|
|
|
What do you mean?
Do you mean is this an assignment from school?
If that is what you meant, no, it is not.
I am aged 31. I am in full time education, reading a degree in biomedical science at a university in the UK, and c# programming is simply a hobby.
aspdotnetdev, Yes, I suppose you have a point. I have indeed bit off more than I can chew. What can I say, I am very impatient, and want to know to much to quick
Time to start from the beginning I suppose.
Thank you everyone.
Kind regards,
Steve
|
|
|
|
|
Assuming you are starting with a number (n in the example below) you can just do:
Convert.ToString(n, 16)
If you have a String rather than an number, convert it to an int first.
Seems a bit of an obvious answer, so maybe I've misunderstood your question.
|
|
|
|
|
This is like the fifth post on the same topic. What wasn't mentioned is that the number can be very large (larger than a long can hold).
|
|
|
|
|
Hi again everyone.
I figured out how to represent a string of digits as its byte encoding representation, by converting c++ code that does just this, but it is producing the wrong result and I dont know why...
Here is the working code from my app...
private void button1_Click(object sender, EventArgs e)
{
string serialNumber = ("123456789");
byte[] output = new byte[5];
int result = decodeSerial(serialNumber, output, 5);
}
static int decodeSerial(string serial, byte[] result, int ResLen)
{
int m;
int digit, aux;
Array.Clear(result, 0, ResLen);
for (int i = 0; i < serial.Length ; i ++)
{
digit = serial[i] - '0';
aux = digit;
m = 0;
do
{
aux += result[m] * 10;
result[m++] = (byte)(aux % 255);
aux >>= 8;
}
while (m < ResLen);
digit *= 2;
}
return 0;
}
}
Now, when I pass in the string "123456789" I should get the byte encoding of:
0x75, 0xBC, 0xD1, 0x05 (Workis on my calculator AND it worked in a c++ app with the ORIGINAL c++ code,
But in this app, it produces the following hex data...
45, 9A, 71, 07
I def want to use this function, as when it works, I will be passing in a string of digits 41 in length, and besides, the original c++ function worked many months before when I tried it, but now I am using c#.
I would also prefer not to use bignum or any other type of function.
Can anyone help out please?
Thank you
Steve
|
|
|
|
|
stephen.darling wrote: while (m < ResLen);
I wasn't going to look at all this in any detail, however that line struck me as a meaningless statement as it repeats nothing until the condition becomes false. You may want to drop the semi-colon, or have to change something else altogether.
|
|
|
|
|
Hi.
What do you mean by 'drop the semi colon'?
If I remove the semi colon, I get an error telling me semi colon is required.
Also, if You dont mind me asking, how does it do nothing, as I step through my code, something is definatly happening, and I do end up with a number of bytes, just not what I get from my calculator.
Thank you again, Luc Pattyn.
Steve
|
|
|
|
|
sorry, my mistake. I overlooked the "do" keyword.
|
|
|
|
|