char to hex conversion

This is a discussion on char to hex conversion within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm searching for the answer to the following task. I have to write a function that gets a ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    char to hex conversion

    Hi everyone,

    I'm searching for the answer to the following task. I have to write a function that gets a string with decimal numbers, all separated by a ",". My job is to convert these numbers to hex and pass them to a special, given print-function. My problem is, that I'm not allowed to use printf or sprintf or any other function from a library.
    Could somebody help me please? Thank!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by alezan
    problem is, that I'm not allowed to use printf or sprintf or any other function from a library.
    Could somebody help me please? Thank!
    Sounds like homework. What have you tried so far? I don't see your attempt, which means you haven't read the forum guidelines. Go do that and try your post again. In the same thread.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    ok, that is what I've done so far. Is there anything I can optimize? One prob is left: I'd like to call int2hex even if there is no , at the end of the input string (e.g. 78, 10); is there a way I can manage this?
    Thanks...

    Code:
    //Calculate Int 2 Hex from String
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void int2hex(int number){
         char hexdigit[] = "0123456789ABCDEF";
         char hexnum[7];
         char buffer;
         int i = 0;
         while(number){
                       buffer = hexdigit[(number % 16)];
                       number /= 16;
                       hexnum[i++] = buffer;
         }
         
         char result[i];
         int l;
         for(l = i-1; l >=0; l--){
               result[l] = hexnum[l];
               printf("%c", result[l]);
         }
         //here: Print(result);
    }
    
    void filter(char *string){
         int decimal = 0;
         while(*string){
                        if(*string == ','){
                             int2hex(decimal);
                             decimal = 0;                                                              
                        }
                        else if(*string == ' '){
                        }
                        else{
                             decimal = decimal*10 + (*string - '0');
                        }  
                        *string++;
         }
    }
    
    int main(int argc, char *argv[]){
      char string [8] = {'7','8',',',' ','1','0',','};
      //int2hex(10000);
      filter(string);
      
      system("PAUSE");	
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  3. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21