marc234
marc234

Reputation:

Convert HEX value to (chars, string, letters and numbers)

I'm programming AVR microcontroller Atmega16 in C.

I don't understand C, i spend my time programming in PHP, and i just don't get it in C.

I don't get also how can i post sourcecode here. If someone know, please fix my tags.

My problem is, i have this function:

unsigned char
convert_sn_to_string (const unsigned char *SN_a /* IDstring */      )
{
  unsigned char i;

  for (i = 0; i < 6; i++)   //6 byte az SN 
    {

 if(SN_a[i]==0x00)
     {
     Send_A_String("00");
     }


    }


  return 1;
}

Inside for loop i can acces 6 byte value by hex.

Inside variable SN_A i can have

SN_A[0]=0x00;
SN_A[1]=0xFF;
SN_A[3]=0xAA;
SN_A[4]=0x11;
(...)

and similar. It could be from 00 to FF.

What i need, is to convert that code to 12 char string. If i have

SN_A[0]=0x00;
SN_A[1]=0xFF;
SN_A[3]=0xAA;
SN_A[4]=0x11;
(...)

I would like get at output

new[0]=0;
new[1]=0;
new[2]=A;
new[3]=A;
new[4]=1;
new[5]=1;
(...)

and so to 12, because i would like change that 6 (double) AA values, to separated.

So then i can do a loop

for i=0 i<12 i++
{
  do_something_with_one_letter(new[i]);
}

Now i can play with that values, i can send them to display, or anything i need.

Upvotes: 1

Views: 1130

Answers (1)

Vatine
Vatine

Reputation: 21288

A hex value is simply another way of writing integers. 0xFF == 255.

So, if you want to "split" them, you need to first decide how you want to split them. This, essentially, decides on the exact way you stuff the split values into your new array.

To split a value, something like this can be used:

hexval = 0x1A
low_nybble = hexval & 0xF
high_nybble = (hexval >> 4) & 0xF

You now have 1 stored in high_nybble and 10 stored in low_nybble.

Upvotes: 1

Related Questions