Posts

Showing posts with the label C

Odds and ends.

1. What is their ip? $ ./getipinfo.sh walmart.com Getting information for domain: walmart.com [ 161.170.244.20 ]... NetRange:       161.163.0.0 - 161.177.255.255 OriginAS:       AS32851 OrgName:        Wal-Mart Stores, Inc. City:           Bentonville Country:        US NetRange:       161.170.0.0 - 161.170.255.255 OriginAS:       OrgName:        Wal-Mart Stores, Inc. City:           Bentonville Country:        US [code]     #!/bin/bash     # A sample shell script to print domain ip address hosting information such as     # Location of server, city, ip address owner, cou...

Pick it for me.

Image
Random numbers can be very important in games. No matter whether you use it for possible outcomes or graphics characters, you can have a bit of fun. If you look for the oracle on this blog, you can one way a random number can be used. loop Print a random ascii character to the screen sequentially without a linefeed. until done. First let's consider freebasic to print out random characters to a screen to make the computer it has gone haywire, it is basically the same code. Some people used to use code similar to this on the old TRS-80 to confuse sales people. [code] for x = 1 to 2000     Randomed = INT(RND * ((255 + 1) - MIN) + MIN)     ?chr$(Randomed); next x [/code] Output finale: Now let us look at how it might be done in C. [code] #include <stdio.h> #include <stdlib.h> int main() {   int c, n;   for (c = 1; c <= 1000; c++) {      n = rand() % 255 + 1;      pr...

Dec 2 hex.

Image
if you wanted to convert a decimal number to a hexadecimal number, you can do it several ways. first you could do it the old fashion way by hand. or you could use a C language program such as: [code]   #include<stdio.h> int main(){     long int decimalNumber,remainder,quotient;     int i=1,j,temp;     char hexadecimalNumber[100];     printf("Enter any decimal number: ");     scanf("%ld",&decimalNumber);     quotient = decimalNumber;     while(quotient!=0){          temp = quotient % 16;       //To convert integer into character       if( temp < 10)            temp =temp + 48;       else          temp = temp + 55;     ...

Another C conversion.

The main thing we did was to add a substitution for get(ch) and clrscr(); Original code: #include<stdio.h> void main() {         int n,d=0,j,a[9];         clrscr();         printf("Enter the Integer which u want to Convert Decimal to Binary : ");         scanf("%d",&n);         while(n>0)                {                     a[d]=n%2;                     n=n/2;                     d++;          ...

QB2C

QB2C has been around for along time. QB2C is a Quickbasic to C translator. So you can take the Old BASIC source and translate it to C for recompiling. For low end machines such as embedded system this can be a boon for not having to reinvent the wheel. Just put it on my Cisco NSLU2. Of course you have to compile the source code on the unit. That took a while but the working programs seem fast enough. There are some quirks as it is not perfect, so you may have to massage some C code for it to work.  $ cat /proc/cpuinfo Processor    : XScale-IXP42x Family rev 1 (v5l) BogoMIPS    : 266.24 Features    : swp half thumb fastmult edsp CPU implementer    : 0x69 CPU architecture: 5TE CPU variant    : 0x0 CPU part    : 0x41f CPU revision    : 1 Hardware    : Linksys NSLU2 But there can be some other advantages. One such advantage is that you can learn C code by comparin...

Freedom of assembly

Image
Assembly language is closer to what a computer understands and less like what a human would understand. but it is fast and compact. when assembled and linked vs a compiled higher level program. Assembly language version of Hello world:   section .data hello: db 'Hello world!',10 ; 'Hello world!' plus a linefeed character helloLen: equ $-hello ; Length of the 'Hello world!' string ; (I'll explain soon) section .text global _start _start: mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output mov ecx,hello ; Put the offset of hello in ecx mov edx,helloLen ; helloLen is a constant, so we don't need to say ; mov edx,[helloLen] to get it's actual value int 80h ; Call the kernel mov eax,1 ; The system call for exit (sys_exit) mov ebx,0 ; Exit with return ...

C code tidbits.

Image
Just like there are many verbal languages for speaking i.e. English, Spanish, Italian or etc.,  computers have their own languages also. Most people understand a verbal language  without having to do any translation. For computers that can be different. So if you want to use a language on a computer, some translation will need to be done to convert the commands (aka source code) to the ones and zeroes a computer understands. This is called compiling. There are a zillion languages that can be used, but one of the most common is "C" pronounced "see". "C" was made famous by Dennis Ritchie and Brian Kernighan also know and K&R. Even if you are not a programmer, you will get the urge to create your own program. This usually happens when you seen some code on a web page or in a magazine that you just have to try. So then every once in a while you may actually need to create a simple program either for testing a system or just to create a quick utility. M...

A bit of logic to code.

Image
Every program should have at least these two points: start and stop. A program is like a todo list. You could use English, Spanish, or a host of other languages to use your todo list. More on logic steps later. So with computer languages you can do the same thing.  Examples: in C #include <stdio.h> main() { } or In basic: rem start rem stop In Bash: #!/bin/bash # start # stop Of course that does not really do anything in that sequence. Now let's go to the beginner's hello world example and see how things change. Yes we are adding a step here in the sequence. Let's just use english for now. Examples: in C #include <stdio.h> main() {         printf("Hello, world.\n"); } In Basic rem start print "Hello, world." rem stop end Bash #!/bin/bash # start echo Hello, world. # stop You can not get any simpler than that for a start, In some ways you are an architect putting rooms together for a house such as a bedroom, bathroom, kitchen, den, l...

A bit of logic to code.

Image
Every program should have at least these two points: start and stop. A program is like a todo list. You could use English, Spanish, or a host of other languages to use your todo list. More on logic steps later. So with computer languages you can do the same thing.  Examples: in C #include <stdio.h> main() { } or In basic: rem start rem stop In Bash: #!/bin/bash # start # stop Of course that does not really do anything in that sequence. Now let's go to the beginner's hello world example and see how things change. Yes we are adding a step here in the sequence. Let's just use english for now. Examples: in C #include <stdio.h> main() {         printf("Hello, world.\n"); } In Basic rem start print "Hello, world." rem stop end Bash #!/bin/bash # start echo Hello, world. # stop You can not get any simpler than that for a start, In some ways you are an architect putt...

Programming without coding.

Image
 ( http://www.youtube.com/watch?v=jxDw-t3XWd0 ) How would you like to create programs without learning some fancy computer language per se. You could create games, animations, educational projects and much much more. MIT has created software called Scratch to do just that. In fact, educational institutions such as Harvard have used  it part of the curriculum for both computer and non-computer science majors to aid them in learning about computers. Here is a quick intro into Scratch. As you can see everything is drag and drop. No need to learn a epic set of commands to do even the most simplest things. Various video sites have a plethora of movies to watch to learn more about Scratch. You might need to learn some basic logic, but that is picked up easily enough.  You can see it is more user friendly that the traditional programing environment. Some traditional programmers use even a more simplistic environment. Even schools such as Harvard have integrated Scratch programmi...