• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drumms Useful Functions!
#1
As I've been programming I've noticed there is a nice collection of functions I've had to repeatedly use. So I thought I'd get some of them to you all. Maybe I could create a header file at some point..

1. Remove Newlines from given char pointer/array
Once you've finished with the string returned from this free it with free(returnedstring);
This string is useful when you may have taken user input, very often it will take all the chars (including the enter aka newline) If you don't want this bit of data and would rather deal with it, you can use this to go through every character until you reach the newline, and change that to 0, which in C is the end of a string.
[code=c]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char* clean( char *cleanme ){
char* newstring = (char*) malloc (strlen(cleanme) + 1);
int i;

for (i = 0; *cleanme != '\n' ; i++, *cleanme++)
newstring[i] = *cleanme;

newstring[i] = '\0';
return newstring;
}[/code]
2. Random IP Generator
I used this as part of an IP source spoofer I was writing, I thought I might as well stick it up here, it shows how powerful sprintf can be. Of course again, you'll want to free the variable once you've finished with it, to avoid a memory leak.
[code=c]
char* randomIP(){

char* buffer = (char*) malloc (15 + 1); // +1 for NULL Char

memset (buffer, 0, 15+1);

int numb[4], i;

for (i = 0; i < 4; i++)

numb[i] = rand() % 255;

sprintf(buffer, "%d.%d.%d.%d\0", numb[0], numb[1], numb[2], numb[3]);

return buffer;

}
[/code]
3. Examine String Data Function
I found myself in need of some code for examining strings, and printing them to output in various different ways. This is great when you're working with data, and start seeing problems with your strings coming up, in the console, you'll often see a strange character, or none at all, because it's how the character works, this will let you view the decimal number of the characters (I'll add in hex and oct when I need it later) so you can examine the string on a much deeper level. This can be used when it may not be convenient to use GDB to examine the data (Which I recommend a go at)
[code=c]
#include <stdio.h>
#include <string.h>

void exdata(char *string, int mode){
int x, len = strlen(string);
if (mode == 1){ // Print Characters and the rest in dec
for (x = 0; x < len; x++){
if (isalnum(string[x]) )
putchar(string[x]);
else
printf("%d", string[x]);
}
}
if (mode == 2){ // Print all chars in decimal
for(x = 0; x < len; x++)
printf("%d", string[x]);
}

}
[/code]
More to come.
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply


Messages In This Thread
Drumms Useful Functions! - by Drumm - 16-04-2011, 01:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [C++] Inline Functions Tutorial Clones 1 8,750 11-09-2012, 11:30 AM
Last Post: annaharris
  Two functions for the ELU Rating system Drumm 8 21,934 02-03-2011, 09:57 AM
Last Post: Drumm

Forum Jump: