Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

15 September 2010

ASCII to hex

Alsalamo Alaykom whoever following me :)

Today I am coming for you with a complete app in C, it is simple but yet complete and do a great job.

For some reason I wanted to convert from ASCII to hex, I know that many Linux app already exist for that (oc, hexdump), but may be I found them hard to use correctly :)

The following is a simple but complete app you can use for that purpose, have fun :) :

/*
* @auther mhewedy
* @email mohammed_a_hewedy@hotmail.com
* @tested under gcc (GCC) 3.4.2 (mingw-special) (quite old :) )
*/

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

char* usage(char*);

int main(int argc, char* argv[])
{
if (argc <= 1)
{
char* usage_string = usage(argv[0]);
printf("%s\n", usage_string);
free(usage_string);
exit(1);
}

int s_option_present =0;
char* string = NULL;

if (strcmp("-s", argv[1]) != 0)
string = argv[1];
else
{
string = argv[2];
s_option_present = 1;
}

while (*string != '\0')
{
printf("%X%s", *string++, (s_option_present? " " : ""));
}
printf("\n");

return 0;
}

char* usage(char* app_name)
{
char* usage = (char*) calloc(40, sizeof(char*));
strcpy(usage, "Usage: ");
strcat(usage, app_name);
strcat(usage, " [-s] {ascii string}");
return usage;
}



Example of usage:
atohex.exe -s ABC
atohex.exe -s 1
atohex.exe hello

14 July 2010

Get bases for decimal nubmers

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

#define SIZE sizeof(unsigned)* CHAR_BIT
//#define DEBUG

int main(void){


char digits[]={'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
char ret[SIZE] = {0};
unsigned n, b;

printf("Enter a Positive number? " );
scanf("%u", &n);
printf("Enter Base? " );
scanf("%u", &b);

int i=0;
do{
ret[i++] = n%b;
n /=b;

}while (n>0);

#ifdef DEBUG
for (i=0; i<SIZE;i++)
printf("%i", ret[i]);
printf("\n");
#endif

// SWAP:
for (i=0; i<SIZE/2;i++){
int tmp=ret[i];
ret[i] = ret[SIZE-i-1];
ret[SIZE-i-1] = tmp;
}

for (i=0; i<SIZE;i++)
printf("%c", digits[ret[i]]);


printf("\n");
return 0;
}

21 June 2010

Two ways to convert decimal numbers to binary strings

I'll show you two ways to convert decimal to binary.

First, the most basic way is apply the following pseudo-code:

int x
char[80] arr
do{
reminder = x%2;
arr[i] = reminder
}while (x/2 > 0);
reverse arr;


And the other is more advanced (and more simple), it use bitwise operations instead:

int x
while (x > 0){
arr[i] = ((x & 1) ? '1' : '0');
x >>=1;
}
reverse arr


and here's a C code for the last one:
 
//I am not the author of this function, I copied from the link below
void dec2binary(long i){

char* str = malloc(sizeof(long)*8*sizeof(char));

char* p = str;
while (i > 0){
*p++ = ((i & 1) ? '1' : '0');
i >>=1;
}

while( p-- != str ) /* print out the result backwards */
printf("%c",*p);

free(str);
}


please see:
http://www.daniweb.com/code/snippet216349.html

24 May 2010

Casting in C vs Java

Al salamo Alykom,

I will talk about casting for primitive types in Java vs in c.

suppose we have two numbers: float f and int i:

1- the following in valid in both languages:
f = i;
2- the following is valid only in c, and will cause compilation error in java:
i = f; // in java, it should be i = (int) f;
3- promotion rules (applies in both):
int x = 10;
float d = x / 3.0f; // result is 3.333333, NOTE, the f of 3.0f only required in Java

int x = 10;
float d = x / 3; // result is 3

int x = 10;
float d = (float)x / 3; // result is 3.333333

20 May 2010

Shift operators in Java

Here's a very simple lesson that illustrate the shift operators in java

http://www.sap-img.com/java/java-bitwise-shift-operators.htm

Summary:

We have three shift operators in java >>, >>> and <<

1- >>
definition:
right shift with the outermost (hi bit) value
example:
9>> 2
9 in binary is 0000000000001001
so, we need to shift right 2 bites adding instead 2 bits of the hi bit
so, result will be 0000000000000010

2- >>>
It is the same as the above, but adds 0 at the left most (not the hi bit)
so, suppose we have
-1 >> 2
-1 in binary is 1111111111111111 (the 2's complement of 1)
so, the result will be -1 also. but if we applied >>> instead, we will always add 0's to the left of the bit pattern.

3- <<
left right is the oposite to the left shift (<<), that add the hi bit from the right.
ex:
9 << 1
9 in binary is 0000000000001001
result will be 0000000000010010 (in decimal is 18 )

(please let me know if there's any errors because I wrote that conclusion in hurry ;) )