C program to get the ascii string from a string

Example diagram of the printf function in the ...
Example diagram of the printf function in the C programming language (Photo credit: Wikipedia)

Lot of times, you would like to get the complete string in hex or ascii format and if you are one of them then this is something that will be helpful for you 🙂

Example output:

lp-amita[d=~/bin]> ./ascii aamit
String – aamit, Length – 5

String : aamit
Hex    : 0x61616d6974
Dec    :   9797109105116

And the source code:

 

/*n* =====================================================================================
*
*       Filename:  ascii.c
*
*    Description:  ascii to dec
*
*        Version:  1.0
*        Created:  03/02/2012 12:08:49 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Amit Agarwal (aka)
*  Last modified: Thu Mar 08, 2012  08:47PM
*
* =====================================================================================
*/
void main (int argc, char **argv){
    int i=0;
    char a[200]="",b[200]="";
    printf("String - %s, Length - %dn",argv[1],strlen(argv[1]));

    while (i < strlen(argv[1])){
        sprintf(a,"%s%x",a,argv[1][i]);
        sprintf(b,"%s%d",b,argv[1][i]);
        i++;
    }
    printf ("n");
    printf ("String : %sn",argv[1]);
    printf ("Hex    : 0x%sn", a);
    printf ("Dec    : %sn", b);
}
Enhanced by Zemanta

Array of all possible colors in bash script

Sometime back, we had looked at using colors in bash script and here is a way to create a array of all the colors.

#!/bin/bash - 
#===============================================================================
#
#          FILE:  colorarray.sh
# 
#         USAGE:  ./colorarray.sh 
# 
#   DESCRIPTION:  
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), [email protected]
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 16/04/12 17:24:09 IST
# Last modified: Mon Apr 16, 2012  05:39PM
#      REVISION:  ---
#===============================================================================
count=0
for i in 40m 41m 42m 43m 44m 45m 46m 47m
do
    for l in 0 1
    do
        echo -n "l=$l "
        for k in 0 1
        do
            # echo -n "k=$k "
            for j in {30..37}
            do
                carr[$count]="\033[$k;$j;$l;$i"
                echo -ne "${carr[$count]}$count \033[00m"
                ((count++))
            done
        done
        echo
    done
    echo
done

You can add this script to any script of your choice and you have all the 256 colors available to you in the script without additional coding. Happy coding.

Enhanced by Zemanta

Convert string to hex.

If you want to use snoop or tcpdump with advanced search in the packet, then you would need to convert the string to hex string. For this, either you can use a web search and find some web application to do that or you can use a simple C program like this 🙂

/*
* =====================================================================================
*
*       Filename:  ascii.c
*
*    Description:  ascii to dec
*
*        Version:  1.0
*        Ceated:  03/02/2012 12:08:49 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Amit Agarwal (aka), [email protected]
*  Last modified: Thu Mar 08, 2012  08:47PM
*
* =====================================================================================
*/
void main (int argc, char **argv){
    int i=0;
    char a[200]="",b[200]="";
    printf("String - %s, Length - %d\n",argv[1],strlen(argv[1]));
    /*printf("%x\n 0x",argv[1]);*/
    while (i < strlen(argv[1])){
        /*printf("%c",argv[1][i]);*/
        sprintf(a,"%s%x",a,argv[1][i]);
        /*printf("a=%s\n",a);
        printf("b=%s\n",b);*/
        sprintf(b,"%s%d",b,argv[1][i]);
        i++;
    }
    printf ("\n");
    printf ("String : %s\n",argv[1]);
    printf ("Hex    : 0x%s\n", a);
    printf ("Dec    : %s\n", b);

}
Enhanced by Zemanta