• Welcome to Valhalla Legends Archive.
 

Another damned pointer issue!

Started by Eli_1, July 16, 2004, 05:04 PM

Previous topic - Next topic

Sargera

#15
Quote from: Eli_1 on July 16, 2004, 07:46 PM
power is my function. I wasn't aware there was a pow function untill after I wrote the function and saw it on google.

Can you please paste your entire program including your driver program (main) so I can help you fix your problem.  Using the pow function requires two floats or two doubles, and I would have to modify your program a bit to support pow(), and oddly enough when I did it crashed.  ^^ But I am assuming it's because of your earlier functions.  So please post the updated functions/driver program.

Eli_1

#16
Hmm, alright...


convert.h
(is suppost to include different conversion methods,
like base2 (bin), base 4, base 16 (hex), and base 64,
but as you can see, I'm stuck on hex. :)


/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
/* |||||||||| Convert.h - base converting functions ||||||||||| */
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */





// ================
// Headers
// ================
#include <string.h>
#include <stdio.h>


// ==========
// Prototypes
// ==========

void hex(long, char*);
void bin(long, char*);

long power(int, int);




// ==============================================
// Functions:
//
// Note: Most functions are void, because I don't
//       know how to return a character array
//       properly. :(
// ==============================================

void hex(long number, char *s1) {

       char             buftmp[256];
       char             *binbuf = new char[256];
       char             hextmp[4];
       char             buffer[256];
       int              i=0;
       int              holdnum=0;


       // ====================================
       // Convert number binary
       // ====================================
   binbuf[0] = 0;
       bin(number, binbuf);


       // ==============
       // Convery to hex
       // ==============

       while (strlen(binbuf) > 0) {
               strncpy(hextmp, binbuf, 4);
               for (i=0; i<4; i++) {
                       if (hextmp[i] == '1') {
                               holdnum += power(2, 4 - (i + 1));
                       }
               }
      

               // =============
               // Add to buffer
               // =============
      
               strcpy(buftmp, buffer);
               if (holdnum < 10) {
                       sprintf(buffer, "%s%d", buftmp, holdnum);
               } else {
                       switch (holdnum) {
                       case 10: sprintf(buffer, "%sA", buftmp); break;
                       case 11: sprintf(buffer, "%sB", buftmp); break;
                       case 12: sprintf(buffer, "%sC", buftmp); break;
                       case 13: sprintf(buffer, "%sD", buftmp); break;
                       case 14: sprintf(buffer, "%sE", buftmp); break;
                       case 15: sprintf(buffer, "%sF", buftmp); break;
                       }
               }
               holdnum = 0;
               binbuf += 4;
       }

       // return
       strcpy(s1, buffer);

}







void bin(long number, char *s1) {
       int              remainder;
   int        counter2 = 0;
       char             bufhold[256];
       char             buffer[256];
   
   
   if (number == 0) {
      strcpy(s1, "00000000\0");
      return;
   }


       while (number > 0) {
               remainder = number % 2;
               number /= 2;

               strcpy(bufhold, buffer);
               sprintf(buffer, "%d%s", remainder, bufhold);
      counter2++;
       }
   
   buffer[counter2] = 0;

       // ==============
       // Pad with zeros
       // ==============

       while (strlen(buffer) % 8 > 0) {
               strcpy(bufhold, buffer);
               sprintf(buffer, "0%s", bufhold);
       }

       // return
       strcpy(s1, buffer);            
}







long power(int base, int exponent) {
       long             holdnum=base;
       int              i;
   
   if (exponent == 0) { return 1; }

       for (i=1; i<exponent; i++) { holdnum = holdnum * base; }
       return holdnum;    
}





//void strtohex(char *s1, char *s2) {
//}

//void strtobin(char *s1, char *s2) {
//}




main.cpp (for testing):

#include <stdio.h>
#include <conio.h>
#include "convert.h"


int main() {
       // test hex
       char test[256];
       hex(255, test);
       printf("0x%s\n", test);

       getch();
       return 0;
}



Zakath

Incidentally, I don't feel that there's anything wrong with strcpy in certain situations. If you have a 256 character array, and you're copying a 30 character string literal to it, it's not going to harm anything.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Banana fanna fo fanna

Then use strncpy with a 256 argument.

Sargera

It's slower because it has to do a checkbound and it's more typing.

Banana fanna fo fanna

And that, my friends, is why IIS has so many holes.

Kp

Quote from: $t0rm on July 23, 2004, 08:23 AMAnd that, my friends, is why IIS has so many holes.

huh?  Zak's exactly right.  strcpy with a hardcoded second argument is just fine, and there's absolutely no reason to go using strncpy in that situation.  Of course, this assumes the code will only be maintained by people who aren't dumb enough to go shrink a buffer indiscriminately, although even shrinkage would be nothing more than an automatic stack corruption / automatic crash when run this code. :P  Besides which, a good compiler can notice that you've hardcoded the constant and inline it as some constant stores.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Banana fanna fo fanna

Oh, I didn't see that he wanted a string constant.