user31965: I need to write a program that takes a user inputted HEX value and converts it to binary, then an LED blinks according to the number of 1's in the binary. For instance, if the value is 0xABC then there are number of 1's are 7 so LED will blink 7 times.
I only just started Arduino so not sure how to let user input a value nor do I know how to convert HEX to BIN but I'm assuming that once I convert the HEX to BIN I can just use a loop to count the number of 1's yet I'm unsure as to actually how to do that. This is what I've written so far.
  byte val=0xABC;
int ledPin=13;

int countSetBits(unsigned int n) 
{ 
    unsigned int c; 
    for (c=0;n>0;n=n&(n-1)) 
        c++; 
    return c; 
}


void blink(int n)
{
    for (int i = 0; i < n; i++)
    {
        digitalWrite(ledPin,HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(ledPin,OUTPUT);
    blink(countSetBits(val));
}

void loop()
{   }

Any help would be appreciated.
EDIT: If I want to take a number from a user and then make the LED blink according to the number of 1's in its binary form, how would I implement that?

user31105: You might do it much simpler... Create a fixed array that for each hex character possible (0..F) has the number of 1's counted.
Than iterate through the string, sum up the values of 1's from the array and make a LED blink that many times.
Something in pseudo code:
// Number of 1's: 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
boolean ones[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, ...                }; 
string str = "ABC";
int total = 0;
for (index = 0; index < str.length; index++)
{
   if string[index] <= '9' 
       total += ones[string[index] - '0'];
   else
       total += ones[string[index] - 'A' + 10];
}


user10394: You could use BIT SHIFTING looks flash, but is actually really useful.
const unsigned char value = 0x55;  // Input
int blinks = 0;                    // Times to blink.

for (int i = 0; i < 8; ++i)
{ 
    blinks += ((value >> i) & 0x01); // Could just blink the light if its true.
}

(value >> i) takes value and moves the number right i places.  So if value is Hex 55 (0x55) that is 01010101 in binary.  On the first time around the loop when i is 0 (value >> 0) will be 0x55 (unchanged).   0x55 & 0x01 is a logical and between the two numbers, so any bits that are set in both will be set in the result.  Since 0x01 only has the least significant bit set (LSB) then we only need to look at the LSB of 0x55, which we know is set.  therefore blinks is incremented by one.
On the second time around the loop value >> i is 0x2A (00101010) because value has been shifted right one place.  So 0x2A & 0x01 = 0, so blink isn't altered.
etc.
The answer at the end of the loop is blinks == 4.

user7508: You are almost there. Now you just have to read and parse the hex
numbers supplied by the user.
The simplest way to accept numeric user input is with
Serial.parseInt(). This is not always ideal since it involves blocking
the program until it either receives an invalid character or the reading
times out. It is also not applicable to your situation because it only
works with decimal numbers.
The better standard way to get the input is to put the incoming bytes
into a buffer until you catch a specific character designated as an end
of message marker. This is typically a CR ('\r' in C++) or an LF
('\n'). This technique is more involved than just calling parseInt()
but it is often preferred because it can be done in a non-blocking way.
See for example this simple command line interpreter.
In your case, however, you do not need to buffer the incoming bytes. You
can instead parse the hex number while you read it, like this:
void loop()
{
    if (Serial.available()) {
        static unsigned int val;
        int c = Serial.read();
        if (c >= '0' && c <= '9') {
            val = 16 * val + (c - '0');
        } else if (c >= 'a' && c <= 'f') {
            val = 16 * val + (c - 'a' + 10);
        } else if (c >= 'A' && c <= 'F') {
            val = 16 * val + (c - 'A' + 10);
        } else {  // End of number.
            blink(countSetBits(val));
            val = 0;  // reset for next round
        }
    }
}


31965: <p>I need to write a program that takes a user inputted HEX value and converts it to binary, then an LED blinks according to the number of 1's in the binary. For instance, if the value is 0xABC then there are number of 1's are 7 so LED will blink 7 times.</p>

<p>I only just started Arduino so not sure how to let user input a value nor do I know how to convert HEX to BIN but I'm assuming that once I convert the HEX to BIN I can just use a loop to count the number of 1's yet I'm unsure as to actually how to do that. This is what I've written so far.</p>

<pre><code>  byte val=0xABC;
int ledPin=13;

int countSetBits(unsigned int n) 
{ 
    unsigned int c; 
    for (c=0;n&gt;0;n=n&amp;(n-1)) 
        c++; 
    return c; 
}


void blink(int n)
{
    for (int i = 0; i &lt; n; i++)
    {
        digitalWrite(ledPin,HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(ledPin,OUTPUT);
    blink(countSetBits(val));
}

void loop()
{   }
</code></pre>

<p>Any help would be appreciated.
EDIT: If I want to take a number from a user and then make the LED blink according to the number of 1's in its binary form, how would I implement that?</p>
 31105: <p>You might do it much simpler... Create a fixed array that for each hex character possible (0..F) has the number of 1's counted.</p>

<p>Than iterate through the string, sum up the values of 1's from the array and make a LED blink that many times.</p>

<p>Something in pseudo code:</p>

<pre><code>// Number of 1's: 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
boolean ones[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, ...                }; 
string str = "ABC";
int total = 0;
for (index = 0; index &lt; str.length; index++)
{
   if string[index] &lt;= '9' 
       total += ones[string[index] - '0'];
   else
       total += ones[string[index] - 'A' + 10];
}
</code></pre>
 10394: <p>You could use <em>BIT SHIFTING</em> looks flash, but is actually really useful.</p>

<pre><code>const unsigned char value = 0x55;  // Input
int blinks = 0;                    // Times to blink.

for (int i = 0; i &lt; 8; ++i)
{ 
    blinks += ((value &gt;&gt; i) &amp; 0x01); // Could just blink the light if its true.
}
</code></pre>

<p><code>(value &gt;&gt; i)</code> takes value and moves the number right <code>i</code> places.  So if value is Hex 55 (0x55) that is 01010101 in binary.  On the first time around the loop when <code>i</code> is 0 <code>(value &gt;&gt; 0)</code> will be 0x55 (unchanged).   <code>0x55 &amp; 0x01</code> is a logical and between the two numbers, so any bits that are set in both will be set in the result.  Since 0x01 only has the least significant bit set (LSB) then we only need to look at the LSB of 0x55, which we know is set.  therefore blinks is incremented by one.
On the second time around the loop <code>value &gt;&gt; i</code> is 0x2A (00101010) because value has been shifted right one place.  So <code>0x2A &amp; 0x01</code> = 0, so blink isn't altered.</p>

<p>etc.</p>

<p>The answer at the end of the loop is <code>blinks == 4</code>.</p>
 7508: <p>You are almost there. Now you just have to read and parse the hex
numbers supplied by the user.</p>

<p>The simplest way to accept numeric user input is with
<code>Serial.parseInt()</code>. This is not always ideal since it involves blocking
the program until it either receives an invalid character or the reading
times out. It is also not applicable to your situation because it only
works with decimal numbers.</p>

<p>The better standard way to get the input is to put the incoming bytes
into a buffer until you catch a specific character designated as an end
of message marker. This is typically a CR (<code>'\r'</code> in C++) or an LF
(<code>'\n'</code>). This technique is more involved than just calling <code>parseInt()</code>
but it is often preferred because it can be done in a non-blocking way.
See for example this simple <a href="https://gist.github.com/edgar-bonet/607b387260388be77e96" rel="nofollow noreferrer">command line interpreter</a>.</p>

<p>In your case, however, you do not need to buffer the incoming bytes. You
can instead parse the hex number while you read it, like this:</p>

<pre class="lang-c++ prettyprint-override"><code>void loop()
{
    if (Serial.available()) {
        static unsigned int val;
        int c = Serial.read();
        if (c &gt;= '0' &amp;&amp; c &lt;= '9') {
            val = 16 * val + (c - '0');
        } else if (c &gt;= 'a' &amp;&amp; c &lt;= 'f') {
            val = 16 * val + (c - 'a' + 10);
        } else if (c &gt;= 'A' &amp;&amp; c &lt;= 'F') {
            val = 16 * val + (c - 'A' + 10);
        } else {  // End of number.
            blink(countSetBits(val));
            val = 0;  // reset for next round
        }
    }
}
</code></pre>
