# How to control a port with an integer number

Dear Forum,
I am using serial commands to control an arduino uno. In my program, I wish to turn on patterns of pin outs on or off simultaneously.

I know this can be done by addressing each port using HEX commands which represent the bit levels for each pin

To commumicate with the arduino, I am able to send an integer value which represents the bit levels (on or off) for each pin on a given port.

For example
pin 8 on would send a value of 1
pin 9 on would send a value of 2
pin 8 and 9 on would send a value of 3
pins 8, 9 and 10 would send a value of 7
pins 8-13 on would send a value of 63
etc.

How can I translate these integer values into suitable HEX values that the arduino will recognise?

Thanks,
Nick

You need to read up on the function “atoi()” which converts an ASCII string (char *) to an integer.

For those interested, I can reply to my own post after a bit of thinking and reading.

I wanted to change an integer representation of an 8 bit number to use it for a port assignment.
For example:

PORTD = B10101000; // sets digital pins 7,5,3 HIGH

What I hadn't realised is that instead of using this bitwise format starting with B, I can just represent this with the integer value which in this case is 2^7 + 2^5 + 2^3 = 168

so I can just use
PORTD = 168

Simples!