Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (VII)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Tue Jun 26, 2012 8:58 am

ChemDog wrote:Alright no more arrays
That's it ;) Was easy, right? :)
ChemDog wrote:So here is without all of the converting
You're still converting... And I've just seen you're XOR-ing the pointers to the strings, not the strings themselves. Please check previous lesson if you didn't. You have to XOR each character of s1 with the respective of s2.
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Thu Jun 28, 2012 4:44 am

m0skit0 wrote:That's it ;) Was easy, right? :)
Ya it was, I just really wanted to use arrays for some reason :lol:
m0skit0 wrote:You have to XOR each character of s1 with the respective of s2.
Aha! I felt like I was doing it all wrong from the start, but with the help of my new friend, the pointer :twisted:, I think I've got it:
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{	
	char* s1 = "buffy";
	char* s2 = "point";
	char s3[10];
	memset(s3, 0, 10);
	*(s3+0) = *(s1+0) ^ *(s2+0);
	*(s3+1) = *(s1+1) ^ *(s2+1);
	*(s3+2) = *(s1+2) ^ *(s2+2);
	*(s3+3) = *(s1+3) ^ *(s2+3);
	*(s3+4) = *(s1+4) ^ *(s2+4);
	s1 = s3;
	printf("0x%x%x%x%x%x\n", *(s1+0),*(s1+1),*(s1+2),*(s1+3),*(s1+4));

	return 0;
}
[/spoiler]
I actually had a dream about this last night :lol:

Thank you for being so patient and guiding me through these!
Advertising

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Thu Jun 28, 2012 10:09 am

ChemDog wrote:I think I've got it
All right, that's it! ;) Just one thing: why declare a 10 character string when you know it's going to be 6-characters wide?
ChemDog wrote:I actually had a dream about this last night :lol:
That's good :mrgreen: That means your brain is starting to assimilate this concepts ;) Now you'll notice that this kind of solutions will pop up faster and clearer in your mind :geek:
ChemDog wrote:Thank you for being so patient and guiding me through these!
Absolutely no problem, this what this courses are for :)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

asgard20032
Posts: 186
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (VII)

Post by asgard20032 » Thu Jun 28, 2012 3:47 pm

ChemDog wrote:
m0skit0 wrote:That's it ;) Was easy, right? :)
Ya it was, I just really wanted to use arrays for some reason :lol:
m0skit0 wrote:You have to XOR each character of s1 with the respective of s2.
Aha! I felt like I was doing it all wrong from the start, but with the help of my new friend, the pointer :twisted:, I think I've got it:
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{	
	char* s1 = "buffy";
	char* s2 = "point";
	char s3[10];
	memset(s3, 0, 10);
	*(s3+0) = *(s1+0) ^ *(s2+0);
	*(s3+1) = *(s1+1) ^ *(s2+1);
	*(s3+2) = *(s1+2) ^ *(s2+2);
	*(s3+3) = *(s1+3) ^ *(s2+3);
	*(s3+4) = *(s1+4) ^ *(s2+4);
	s1 = s3;
	printf("0x%x%x%x%x%x\n", *(s1+0),*(s1+1),*(s1+2),*(s1+3),*(s1+4));

	return 0;
}
[/spoiler]
I actually had a dream about this last night :lol:

Thank you for being so patient and guiding me through these!
Now do it with a dynamic allocation, and use a loop instead of writing lot of variable assignment. Normally, we use i, j, k for the name of variable for that purpose, so basically inside your loop, you will do *(s3+i)=*(s1+i) ^ *(s2+i).

Another idea for improvement, create a function for that, for example, you could create it like this: char* my_encrypt(char * s1, char* s2, int nElementToXor); return the address of s3 upon success (you have to malloc s3 inside your function) and if the malloc fail, return 0. So easy to know if your function work or not simply by its return.

Because normally, in programming, we prefear breaking a program in function, more easy to maintain, to read, and every function is reusable in another program.
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Thu Jun 28, 2012 3:51 pm

asgard20032 wrote:use a loop instead of writing lot of variable assignment
No. We haven't seen loops in this course yet...
asgard20032 wrote:Another idea for improvement, create a function for that
Nope again. We haven't seen functions yet either...
asgard20032 wrote:Because normally, in programming, we prefear breaking a program in function, more easy to maintain, to read, and every function is reusable in another program.
Totally agree, but this will come later ;) IMHO first it's a good thing to get used manipulating data types and structures so to get a good grasp at this before moving on. This is pretty much unusual in programming tutorials because pointers are shown on more advanced chapters, like they were an advanced feature. I don't think this is right, since pointers are not that hard to understand if taught properly ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

asgard20032
Posts: 186
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (VII)

Post by asgard20032 » Thu Jun 28, 2012 5:45 pm

m0skit0 wrote:
asgard20032 wrote:use a loop instead of writing lot of variable assignment
No. We haven't seen loops in this course yet...
asgard20032 wrote:Another idea for improvement, create a function for that
Nope again. We haven't seen functions yet either...
asgard20032 wrote:Because normally, in programming, we prefear breaking a program in function, more easy to maintain, to read, and every function is reusable in another program.
Totally agree, but this will come later ;) IMHO first it's a good thing to get used manipulating data types and structures so to get a good grasp at this before moving on. This is pretty much unusual in programming tutorials because pointers are shown on more advanced chapters, like they were an advanced feature. I don't think this is right, since pointers are not that hard to understand if taught properly ;)
But what i proposed could be a good exercise for when he move on on the next chapter.
Image

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Fri Jun 29, 2012 5:51 am

m0skit0 wrote:why declare a 10 character string when you know it's going to be 6-characters wide?
At first I declared it to 5, but I was having an issue when getting s1 back to its original state. I would print s1 and get "buffy" but with some junk characters at the end, so I spent a long time just tweaking things. In my dream was where I thought to change it from 5 to 6 but it did not change anything when I actually tried it. When I changed s3 from 6 to 10 it seemed to fix the issue.

Ok, so I went back and declared s3 to 6 and there was no issue. I think what happened was that I changed it from s3[5] to s3[6] and forgot to save before compiling it again :oops:, because I changed it back to s3[5] and I get the junk characters again.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Fri Jun 29, 2012 7:41 am

asgard20032 wrote:But what i proposed could be a good exercise for when he move on on the next chapter.
Of course, but you could post that on the next chapter :mrgreen:

@ChemDog: 5 does not work because if you recall from string manipulation chapter (I strongly suggest you read it if you did not), strings in C have a NUL terminator (NUL ASCII character, code 0, representation '\0') which indicates the end of the string. Thus if you declare s1[5] and put "Hello" into it, you have no space for NUL terminator. Thus when for example printf() goes to print that s1 string, it does so until it finds a NUL character, wherever this is. The junk is the values found after s1 and until a NUL is found.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Mon Jul 02, 2012 12:15 am

m0skit0 wrote: 5 does not work because if you recall from string manipulation chapter (I strongly suggest you read it if you did not)
Ya I eventually remembered, I just didn't think of it when I was first writing it. I am going to go back and reread all of them just to make sure I didn't miss anything the first time. I usually understand what I'm reading better after trying it out.

SifJar
Posts: 251
Joined: Tue Jan 11, 2011 10:19 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by SifJar » Sat Mar 09, 2013 7:20 pm

Well, here are my solutions. If anyone happens to read this, I'd appreciate any comments on the correctness/incorrectness of my code.

Using only pointers: declare an array of signed integers and assing it a size of 50 integers.
(I'm not sure if I fully understood this exercise, but here's what I got anyway:)
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int* example = (int*) malloc(50*sizeof(int));
   free(example);
   return 0;
}
[/spoiler]

Same as the last exercise but also assign positions 5, 6, and 8, and then print them.
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int* example = (int*) malloc(50*sizeof(int));
   *(example + 5) = 10;
   *(example + 6) = 12;
   *(example + 8) = 20;
   printf("%d, %d, %d", example[5], example[6], example[8]);
   free(example);
   return 0;
}
[/spoiler]

Using only pointers: given a random string, print from the 5th character onwards.
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char* random = "this is a random string";
   char* print = random + 4;
   printf("%s", print);
   return 0;
}
[/spoiler]

Given 2 strings of length 5, encrypt the first one using the second one using an XOR operation.
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char* string0 = "abcde";
	char* string1 = "qwert";
	char* encrypted = malloc(6);
	memset(encrypted, 0, 6);
	*encrypted = *string0 ^  *string1;
	*(encrypted+1) =  *(string0+1) ^  *(string1+1);
	*(encrypted+2) =  *(string0+2) ^  *(string1+2);
	*(encrypted+3) =  *(string0+3) ^  *(string1+3);
	*(encrypted+4) =  *(string0+4) ^  *(string1+4);
	printf("%s\n", encrypted);
	free(encrypted);
	return 0;
}
[/spoiler]

This one doesn't quite work as I think it is supposed to. However, I think it does sort of work. The integer value of each character is XORed with the integer value of the corresponding character in the second string. As it happens, the results are not valid characters when printed as characters. But XORing them again with the same characters from the second string does return the original string. e.g.
[spoiler]

Code: Select all

	char* dec = malloc(6);
	*dec =  *encrypted ^  *string1;
	*(dec+1) = *(encrypted+1) ^ *(string1+1);
	*(dec+2) = *(encrypted+2) ^ *(string1+2);
	*(dec+3) = *(encrypted+3) ^ *(string1+3);
	*(dec+4) = *(encrypted+4) ^ *(string1+4);
	printf("%s", dec);
	free(dec);
[/spoiler]
I think maybe I didn't fully understand this exercise. I'm guessing I was not meant to XOR each character with the corresponding character from the second string. However, I am not altogether sure what I was meant to do. Guess I'll have a look at other people's solutions.

EDIT: Looking at other solutions, and the comments made on them, I think these are correct. I fixed my last solution because before I cast each char as an int before XORing them, but m0skit0 pointed out earlier in the thread that that is unnecessary and wastes memory. Still, any more comments on my code are welcome.

Post Reply

Return to “Programming and Security”