• 0

[C] Decimal to Hexadecimal conversion as string help!


Question

I need to convert a decimal number into a hexadecimal number with "0x" beforehand as the program I am writing needs to be able to create a bitmap image by writing in each hex value. I have written th

My current code is shown below.



#include <stdio.h>
#include <math.h>

void decToHex(int decimal);

char bitCode[4]="0x";

int main (int argc, const char * argv[])
{
decToHex(254);
puts(bitCode);
return 0;
}

void decToHex(int decimal){
char hex[5];
char hexArray[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int i=1;
while(decimal>0){
hex[i]=decimal%16;
hex[i]=hexArray[hex[i]];
decimal=floor(decimal/16);
i--;
}
bitCode[2]=hex[0];
bitCode[3]=hex[1];
}
[/CODE]

However when I want to put my bitCode into an array as follows:

[CODE]
unsigned char drawing[]={bitCode,bitCode,bitCode};
[/CODE]

I get an error that says:[b]incompatible pointer to integer conversion initializing 'unsigned char' with an expression of type char[4][/b]

I think i need to somehow convert it to a string to work. Please tell me what i am doing wrong!

3 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.