![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 19
| Explain this C code in english I need someone to look at this c code and explain what is going on in plain english. I would like to maybe convert the C code into something identical that could be used in visual basic (the programming level im currently at -_-). The code is: Code: int gs_chresp_num(char *challenge) {
int num = 0;
while(*challenge) {
num = *challenge - (num * 0x63306CE7);
challenge++;
}
return(num);
}
What I am looking for is some Visual Basic code that will do exactly what this c code does. Or maybe just an explanation in plain english of what is going on in the C code, so I could write my own VB code to duplicate it. Thank you very much for the help! |
| soadlink is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > while(*challenge) challenge++; This steps through the string, one character at a time. > *challenge This returns the numeric value of the current character, say 'A' being 65 > (num * 0x63306CE7) 0x is used to signify a number written in base-16
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Aug 2006
Posts: 19
| Thanks for the reply. That helped a tiny bit, but I still don't have an idea of where to start on the VB side. If I understand you correctly, this code goes through the 'challenge' string (we'll say the string is ABCDEFGHIL) 1 character at a time, and converts each character to a number. And the conversion happens here I assume: num * 0x63306CE7 I know you said 0x represents base16, but what is that 63306CE7? I guess im still completely lost on how it get's ABCDEFGHIL to 884373937. |
| soadlink is offline | |
| | #4 | |
| Gawking at stupidity Join Date: Jul 2004
Posts: 2,289
| Quote:
Just step through it in your head or on paper. The first time through the loop, num is 0 and *challenge is 'A' or 65. It sets the num to 65 - (0 * 1664117991) which is just 65. The second time through the loop, num is 65 and *challenge is 'B' or 66. Keep going from there until you reach the end of the challenge string.
__________________ If you understand what you're doing, you're not learning anything. Ignore any "advice" esbo tries to give you. It's wrong. | |
| itsme86 is offline | |
| | #5 |
| Registered User Join Date: Aug 2006
Posts: 19
| Ok so if the first pass is: 65 - (0*1664117991) Would the second pass be: 66 - (65*1664117991)? If so, then my output on the second pass would be a huge number. And once I do each pass, am I adding up the numbers then? I've been scribbling on paper like you said for the last 10 mins trying to figure this out x_x ..sorry. |
| soadlink is offline | |
| | #6 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| It would indeed be a very large (negative) number.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #7 |
| Registered User Join Date: Aug 2006
Posts: 19
| What am I doing with each of these numbers on every pass after they are calculated? |
| soadlink is offline | |
| | #8 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| You're assigning the result of the expression to num. Code: num = *challenge - (num * 0x63306CE7); Code: bignum = num * 1664117991 result = letter - bignum num = result
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #9 | |
| Fountain of knowledge. Join Date: May 2006
Posts: 662
| Quote:
a certain size depending on the computers word length for an integer, this can vary on different computers, it might be 16 bit on on some older computers, 32 bit (probably) on current computers, and maybe 64 bit on 64 bit machines, AMD64 for example. Anyway you need to sort out the word lengths and bit sizes etc (not too sure of them my self!!) and ensure they are the same in visual basic (or make modifications to enuse this). Basically it takes an input string and generates a code based upon the 'key' 0x63306CE7 and the input string (I think this is known as a 'hash key' but, whatever it generates a code as output). It should be fairly easy to get the loop going in VB you just need to make sure the data sizes work to produce the same result. | |
| esbo is offline | |
| | #10 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Yes, generating such large numbers would cause the value to wrap pretty quickly, so you'd probably want to use a 32-bit integer in VB (assuming that's what the original author of the C code intended).
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #11 |
| Registered User Join Date: Aug 2006
Posts: 19
| Ok I wrote up some code in VB to calculate the string for ABCDEFGHIL: Code: Text2.Text = 65 - (0 * 1664117991) Text2.Text = 66 - (Text2.Text * 1664117991) Text2.Text = 67 - (Text2.Text * 1664117991) Text2.Text = 68 - (Text2.Text * 1664117991) Text2.Text = 69 - (Text2.Text * 1664117991) Text2.Text = 70 - (Text2.Text * 1664117991) Text2.Text = 71 - (Text2.Text * 1664117991) Text2.Text = 71 - (Text2.Text * 1664117991) Text2.Text = 72 - (Text2.Text * 1664117991) Text2.Text = 76 - (Text2.Text * 1664117991) Now that is a huge number as we were all saying it would be, but I am clueless on how to get it to "return a 32bit integer." I hate having to ask someone to just do it for me, but I think it's coming to that . Does anyone know what VB code would be used to get it to return smaller values (32 bit integers?)? Is my calculation correct, but I am just not writing it to return a 32 bit integer? Thanks for all the help so far |
| soadlink is offline | |
| | #12 | |
| Fountain of knowledge. Join Date: May 2006
Posts: 662
| Quote:
good to you, what you want is the lower order numbers. You have someting like 636165918672933??????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????XXXXXXXX The bit you are interested in is the XXXXXXXXX, however you have lost those bits because you are using floating point, I presume, you need to define your result as an integer (a 32 bit one) then you might be getting somewhere (assuming the rest of the code is correct!!). If you subtract the number by which the result is over the max value of a 32 bit int on each iteration of the loop you might be getting somewhere!! (That is 4294967269 (I think not far off anyway!!)). But I don't know VB's data types or your program so I can't be of much more help. however you will get nowhere using floating point numbers, try defining your result variable as an integer and it just might give you the correct answer!! What is actualy happening is a form of encyption (or it can be used as). A good understanding of maths would be helpful to you. Actually I am not even sure if you can have an iinterger variable in VB but I am not sure, however, if you were really good at maths (like me ) you could probably simulate a way of doing it!!If I get really bored I will tell you how (poker tables a bit quiet at the moment) anyway good luck!! You need good luck in poker too!!! | |
| esbo is offline | |
| | #13 |
| Registered User Join Date: Aug 2006
Posts: 19
| I was looking for a way to convert the end result to a 'long' integer in visual basic so i could see the XXXXXXXX part of the number as you were talking about. I tried: Code: text3.text = CLng(text2.text) So i tried: Code: text3.text = CInt(text2.text) I think I'm getting close, just need the right code, or a way to display it at 32bit . Any more tips!?Thanks |
| soadlink is offline | |
| | #14 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| CLng() perhaps? Few seconds on the MSDN gives you all you need to know. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #15 | |
| Super Moderator Join Date: Aug 2001
Posts: 7,472
| The 'overflow' means the VB data type you specified cannot hold the specified value. In C this would not flag an error, but you would lose precision in your value and when you printed it out.....it would not be what you expected. VB catches this and flags an error. The crappy solution is to use VB's crappy error handling and use this: Code: sub Foo(<param_list>)
on local error goto ERR:
'attempt to convert to long here
'due to previous line any error will transfer execution to ERR: label
end sub
ERR:
'Find error number
int errornum=<some function to get number designation of error>;
select case errornum:
case <overflow error number>:
'handle error and resume
resume
end select
There are times when VB will simply allow the loss in precision and not flag an error. However, I found with very large values it does flag an overflow error. There should be a VB function to convert from double to long and long double to long but you will lose precision. The correct approach in VB would be to compute the value, isolate the needed value, place that value into a long. Also note that ALL numeric data types in VB are SIGNED. VB and BASIC do not allow UNSIGNED data types. So the PRINT functions and all VB functions interpret the numeric types as SIGNED. 32-bit integers in 32-bit C:
32-bit integers in 32-bit VB:
I don't think VB changes the data width of int to correspond to the default integer of the platform. Quote:
__________________ If you aim at everything you will hit something but you won't know what it is. Last edited by Bubba; 08-31-2006 at 12:17 AM. | |
| Bubba is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can someone explain this code? | Sharke | C++ Programming | 3 | 06-18-2009 12:12 PM |
| Could someone explain this code for me please... | JoshR | C++ Programming | 89 | 06-26-2005 01:20 AM |
| Seems like correct code, but results are not right... | OmniMirror | C Programming | 4 | 02-13-2003 01:33 PM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |
| Can you have nested code block? What does the compiler do? For example ... | albertr | C Programming | 4 | 01-16-2002 12:04 AM |