# Thread: char to hex conversion

1. ## 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. 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. 3. 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;
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  = {'7','8',',',' ','1','0',','};
//int2hex(10000);
filter(string);

system("PAUSE");
return 0;
}``` Popular pages Recent additions 