Showing posts with label hex. Show all posts
Showing posts with label hex. 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

22 July 2010

Print unicode values of non-ascii characters


class GetUnicode {

static final char digits[] = {'0', '1', '2', '3','4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

public static void main(String[] args) {
String name = "محمد هويدي";
for (char ch : name.toCharArray())
if (Character.isWhitespace(ch))
System.out.print(ch);
else
System.out.print("\\u" + getHex(ch));
}
static String getHex(int ch) {
char tmp[] = new char[32];
int i, j, count =0;

for (i=0; i<32; i++)
tmp[i] = '0';
while (ch>0)
{
int t = ch&0xf;
ch>>=4;
tmp[count++] = digits[t];
}
char ret[] = new char[count +=(4-count&3)]; // make ret size to be multiple of four

for (i=count-1, j=0; i>=0; i--, j++)
ret[j] = tmp[i];

tmp = null;
return new String(ret);
}
}

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

18 May 2010

Convert from Hex to Binary by eye

Here's a simple way to convert from Hexdecimals to Binary just by eye.
suppose you have the following hex expression:

int x = 0xb123f;

and you need to know the binary (or even decimal) representation of this integer.

first, divide this integer into 2 characters (hence, 2 characters in Hex = 1 byte) from right:
so, it will be:
b 12 3f
then write the binary representation for each symbol:
b (11 in decimal) = 1011
1 = 0001
2 = 0010
3 = 0011
f = (15 in decimal) 1111
so, you have the following bit pattern:
0000 1011 0001 0010 0011 1111 (= 725567 in decimal)

note: hence we have 5 hex symbols, we added 0 to the left, and this explains why we add 0000 at the left.


thats all.