C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-30-2006, 11:40 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 19
Explain this C code in english

Hey, first time poster, and I have never even programmed with C so go easy on me, you'll see what I'm after in a sec.

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);
}
I know the code is supposed to take a string of text known as 'challenge' and process it into a new number. And I know for a fact that if you made 'challenge' the string ABCDEFGHIL, the code would output 884373937. I know this because I got the code from here: http://aluigi.altervista.org/papers/gs_chresp_num.h

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   Reply With Quote
Old 08-30-2006, 11:44 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 08-30-2006, 12:21 PM   #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   Reply With Quote
Old 08-30-2006, 12:28 PM   #4
Gawking at stupidity
 
Join Date: Jul 2004
Posts: 2,289
Quote:
I know you said 0x represents base16, but what is that 63306CE7?
It's just a number. It's 1664117991 in base-10 (decimal).

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   Reply With Quote
Old 08-30-2006, 01:10 PM   #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   Reply With Quote
Old 08-30-2006, 01:28 PM   #6
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 08-30-2006, 01:39 PM   #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   Reply With Quote
Old 08-30-2006, 01:41 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
You're assigning the result of the expression to num.
Code:
num = *challenge - (num * 0x63306CE7);
in pseudo code might look like this:
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   Reply With Quote
Old 08-30-2006, 02:03 PM   #9
Fountain of knowledge.
 
Join Date: May 2006
Posts: 662
Quote:
Originally Posted by soadlink
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.
Something to remember is that program always returns an int, which can only be of
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   Reply With Quote
Old 08-30-2006, 02:05 PM   #10
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 08-30-2006, 09:43 PM   #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)
The code works perfectly, and it returns a final value of: -6.36165918672933E+84 in the text2.text box. This is the same value I got when doing it on my calculator manually.

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   Reply With Quote
Old 08-30-2006, 10:55 PM   #12
Fountain of knowledge.
 
Join Date: May 2006
Posts: 662
Quote:
Originally Posted by soadlink
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)
The code works perfectly, and it returns a final value of: -6.36165918672933E+84 in the text2.text box. This is the same value I got when doing it on my calculator manually.

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
I have never used VB so I can't help you a lot, however the value you came up with is no
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   Reply With Quote
Old 08-30-2006, 11:35 PM   #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)
^ which would make the text3 textbox display the long version of my end result. It gave me an 'overflow' error.

So i tried:
Code:
text3.text = CInt(text2.text)
and I got the same thing.

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   Reply With Quote
Old 08-30-2006, 11:42 PM   #14
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 08-31-2006, 12:04 AM   #15
Super Moderator
 
Bubba's Avatar
 
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
VB data types and conversions from one type to another are not as powerful, flexible, or easy as C.

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:
  • DWORD (unsigned int/long)
  • unsigned/signed int
  • unsigned/signed long

32-bit integers in 32-bit VB:
  • dim value as long

I don't think VB changes the data width of int to correspond to the default integer of the platform.

Quote:
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 )
Ok...
__________________
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22