Thread: From It DEcimal them to esadecimale

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    44

    Unhappy From It DEcimal them to esadecimale

    you know an algorithm that converts numbers DEcimal them in esadecimali?
    http://linuxdesktop.it

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I guess esadecimali is Italian for hexadecimal? Hexadecimal and decimal are number representations, they represent the same number but use another base. So there is no need for conversion.

    Can you give an example of what you want? To help you better we need an example of input and output to see what you want to do.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    this is my stupid programm Crypt
    this program in output is print a decimal number

    Code:
    /* 
    Name: CRYPT  
    Author: KRAKZ 
    Date: 12/02/03 14.39 
    Description: CRITTOGRAFIA
    LICENZA : GNU|GPL 
    VERSIONE : 0.2
    */ 
    
     
    
    # include <stdio.h>
    # include <string.h>
    #include <stdlib.h>
    # define MAX 255
    
    int main (void)
    {
      int i, I = 0 , countchiave , countfrase , crypt[MAX] , crypt2[MAX] ,crypt1[MAX] ;
      char frase[MAX] , frase1[MAX], chiave[MAX] ;
          
       printf ("Inserisci frase\n") ; 
       gets(frase) ;
       printf ("Inserisci chiave\n") ;
       gets(chiave) ;
      
    
        countfrase  = strlen(frase)  ;
        countchiave = strlen(chiave) ;
        
        ;
        for ( i = 0 ; countfrase > i ; i++)
        {
        
        if ( countchiave < I)
        I = 0 ;;
        crypt1[i] = chiave [I] ;
        crypt2[i] = frase[i]  ;
        crypt[i] =  crypt1[i] + crypt2[i] ;
        if ( crypt[i] < 100 )
        printf ("0") ;;
        printf ("%d" , crypt[i]) ;
        I = 1 + I ;
        } 
        printf ("\n") ;
        system("PAUSE");  
    
    }  
    
    
    this  is a Decrypt
    
    is riceved the number of crypt and key and decrypt
    Code:
    /* 
    Name: dECRYPT 
    Author: KRAKZ 
    Date: 12/02/03 14.39 
    Description: DeCRPTA 
    LICENZA : GNU|GPL 
    VERSIONE : 0.2 
    */ 
    
    
    
    # include <stdio.h> 
    # include <string.h> 
    #include <stdlib.h> 
    # define MAX 255 
    
    int main (void) 
    { int I=0 , i , countfrase , countchiave , dEcrypt[MAX] , de = 0 ; 
    char frase[MAX] , chiave[MAX] ; 
    
    
    
    printf ("Inserisci File Cryptato\n") ; 
    gets(frase) ; 
    printf ("Inserisci chiave\n") ; 
    gets(chiave) ; 
    
    
    countfrase = strlen(frase) ; 
    countchiave = strlen(chiave) ; 
    
    for ( i = 0 ; countfrase > i ; i = 3 + i) 
    { ; 
    if ( countchiave < I) 
    I = 0 ;; 
    
    
    dEcrypt[de] = (frase[i]* 100 + frase [i+1] * 10 + frase [i+2] +48 ) - chiave [I] ; 
    printf ("%c" , dEcrypt[de]) ; 
    I = 1 + I ; 
    de = de + 1 ; 
    
    } 
    printf ("\n") ; 
    
    system("PAUSE"); 
    
    }
    for optimizer mi code , i mind use of Hexadecimal in substitution to it decimal them






    P.S : My English is terrible ;( ;( ;(
    http://linuxdesktop.it

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    help me
    http://linuxdesktop.it

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - Don't use gets()
    - Don't bump your threads
    - What exactly is your question again?

    >>My English is terrible
    Your're not kidding Try explaining by using a simple example of what you want to do.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    #include <stdio.h>



    main ()
    {

    int decimal , Hexadecimal ;
    printf ("Enter Hexadecimal number :\n") ;
    scanf ("%d" , Hexadecimal) ;



    /* in this point algorithm of conversation of number */
    /*................................................. .*/



    printf ("%d" , decimal ) ;

    }
    http://linuxdesktop.it

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Okay, I understand now, I think. You have an integer number as input, like 123. This is not 123 in decimal, but 123 in hexadecimal right? So in decimal it is 291.

    In that case, realise that the base of the hexadecimal system is 16. To convert it to decimal you need to apply this conversion:

    1 * 16^2 + 2 * 16^1 + 3 * 16^0 = 291

    So in general when you have a hexadecimal value:

    h[n] h[n-1] ... h[2] h[1] h[0]

    You need to perform the conversion:

    h[n] * 16^[n] + h[n-1] * 16^[n-1] + ... + h[1] * 16^1 + h[0] * 16^0

    Hope this helps.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    Originally posted by Shiro
    Okay, I understand now, I think. You have an integer number as input, like 123. This is not 123 in decimal, but 123 in hexadecimal right? So in decimal it is 291.

    In that case, realise that the base of the hexadecimal system is 16. To convert it to decimal you need to apply this conversion:

    1 * 16^2 + 2 * 16^1 + 3 * 16^0 = 291

    So in general when you have a hexadecimal value:

    h[n] h[n-1] ... h[2] h[1] h[0]

    You need to perform the conversion:

    h[n] * 16^[n] + h[n-1] * 16^[n-1] + ... + h[1] * 16^1 + h[0] * 16^0

    Hope this helps.

    scuse me

    but I have

    Example : FE1 (HEXADECIMAL) = 4065 (DECIMAL)

    F * 16^2 + E* 16^1 + 1 * 16^0 = 4065

    what algorithm for convert "F" (HEXADECIMAL) in 15.

    15 * 16^2 + 14 * 16^1 + 1 * 16 ^ 0 = 4065.



    P.S: Scuse me for my bad english and for my stupid questions
    http://linuxdesktop.it

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one way:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      long int num;
      
      printf ("Enter a hex number: ");
      if (scanf("%x", &num) == 1)
        printf ("Thats %ld in decimal\n", num);
        
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You were using %d, which confused me, because then you cannot enter an input like FE1. You can do that if you use %x, like Hammer showed. To store it in a variable, you could also consider using the function sprintf().

    #include <stdio.h>
    int sprintf (char * restrict s, const char * restrict format, ...);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2008, 12:39 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM