Please I need a help only in ansi c.

I have an array of hexa values, for example:
int N=3;
char myHexas[N];
myHexas[0]=0x12;
myHexas[1]=0x34;
myHexas[2]=0xAB;

I need to have a new array with the following:
myStrings[N*2];
myStrings[0]='1';
myStrings[1]='2';
myStrings[2]='3';
myStrings[3]='4';
myStrings[4]='A';
myStrings[5]='B';

Any ideas should be great.

## All 4 Replies

Look into the modulus (%) operator.

Thanks, I almost solve the problem by this way ...

int N=3;
char myHexas[N];
myHexas[0]=0x12;
myHexas[1]=0x34;
myHexas[2]=0xAB;

myStrings[N*2];

sprintf(myStrings, "%2X%2X%2X", myHexas[0], myHexas[1], myHexas[2]);

Ok, myStrings are as follows ...
myStrings[0]='1';
myStrings[1]='2';
myStrings[2]='3';
myStrings[3]='4';
myStrings[4]='A';
myStrings[5]='B';

here myStrings are well as I wanted, GREAT, but if i put ...
myHexas[0]=0x12;
myHexas[1]=0x34;
myHexas[2]=0x00;
myHexas[3]=0xAB;
and then
sprintf(myStrings, "%2X%2X%2X%2X", myHexas[0], myHexas[1], myHexas[2], myHexas[3]);

it results in ...

myStrings[0]='1';
myStrings[1]='2';
myStrings[2]='3';
myStrings[3]='4';
myStrings[4]=' ';
myStrings[5]='A';
myStrings[6]='B';

and I want this result ...

myStrings[0]='1';
myStrings[1]='2';
myStrings[2]='3';
myStrings[3]='4';
myStrings[4]='0';
myStrings[5]='0';
myStrings[6]='A';
myStrings[7]='B';

Any help would be greatly appreciated.
Thanks Juan.

Try:

``sprintf(myStrings, "%02X%02X%02X%02X", myHexas[0], myHexas[1], myHexas[2], myHexas[3]);``

And use unsigned char for your data type.

Thanks, the solution is:

``````int i;
int n = 4;
unsigned char myHexas[n];
unsigned char myStrings[n*2];
unsigned char bufferTemp[2];

myHexas[0]=0x12;
myHexas[1]=0x34;
myHexas[2]=0x00;
myHexas[3]=0xAB;

for (i=0; i<n; i++) {
sprintf(bufferTemp, "%02X", myHexas[i]);
strcat(myStrings, bufferTemp);
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.