user26818: I am doing some byte manipulation in Arduino, but when I enter an integer into the serial monitor, such as 1, and my program stores it as byte incomingByte, the value stored in incomingByte is 49 and not 1. How do I enter the ASCII equivalent of an integer?

user28033: You can do this little trick:
void loop()    
{
while (Serial.available() == 0); //waiting for input    
int val = Serial.parseInt(); //read as integer
}


user24169: Check out the ASCII table.
When you enter 1 into the Serial Monitor you are telling it to send the (ASCII) character 1 - which is represented by the decimal value 49, so the byte that is sent is 49.
I don't believe it is possible to enter the decimal value 1 into the Serial Monitor (you'd have to find some way of entering SOH via the keyboard), so the only option is to recognize and accommodate that on the Arduino end.
The usual way to do this is to subtract 48 from the received value. However this is typically written as - '0' which is actually subtract the ASCII value of 0 (this helps indicate that you converting from ASCII)
So...
int intVal = incomingByte -'0';
... should work fine for you if you are receiving single digit integers.
However, if you also want to accommodate multi digit integers (e.g. 123) then it becomes more complex requiring converting each byte to it's integer equivalent and then multiplying it by 10 to the power of it's position/column...
123 = (1 * 10^2) + (2 * 10^1) + (3 * 10^0)
This is a fun little coding task in itself, but if you're not into that kind of thing then parseInt('123'); will get you going faster...

user27653: Using the Arduino IDE Serial Monitor, there's not much you can do. But you can use a different serial-port terminal such as RealTerm (for Windows, if that's your thing) which allows for a wide array of serial port nitty-gritty details.

26818: <p>I am doing some byte manipulation in Arduino, but when I enter an integer into the serial monitor, such as <code>1</code>, and my program stores it as <code>byte incomingByte</code>, the value stored in <code>incomingByte</code> is 49 and not <code>1</code>. How do I enter the ASCII equivalent of an integer?</p>
 28033: <p>You can do this little trick:</p>

<pre><code>void loop()    
{
while (Serial.available() == 0); //waiting for input    
int val = Serial.parseInt(); //read as integer
}
</code></pre>
 24169: <p>Check out the <a href="http://www.asciitable.com/" rel="nofollow noreferrer">ASCII table</a>.</p>

<p>When you enter <code>1</code> into the Serial Monitor you are telling it to send the (ASCII) <em>character</em> <code>1</code> - which is represented by the decimal value 49, so the <code>byte</code> that is sent is <code>49</code>.</p>

<p>I don't believe it is possible to enter the decimal value <code>1</code> into the Serial Monitor (you'd have to find some way of entering <code>SOH</code> via the keyboard), so the only option is to recognize and accommodate that on the Arduino end.</p>

<p>The usual way to do this is to subtract 48 from the received value. However this is typically written as <code>- '0'</code> which is actually <em>subtract the ASCII value of 0</em> (this helps indicate that you converting from ASCII)</p>

<p>So...</p>

<p><code>int intVal = incomingByte -'0';</code></p>

<p>... should work fine for you if you are receiving single digit integers.
However, if you also want to accommodate multi digit integers (e.g. 123) then it becomes more complex requiring converting each byte to it's integer equivalent and then multiplying it by 10 to the power of it's position/column...</p>

<p><code>123 = (1 * 10^2) + (2 * 10^1) + (3 * 10^0)</code></p>

<p>This is a fun little coding task in itself, but if you're not into that kind of thing then <code>parseInt('123');</code> will get you going faster...</p>
 27653: <p>Using the Arduino IDE Serial Monitor, there's not much you can do. But you can use a different serial-port terminal such as <a href="http://realterm.sourceforge.net/" rel="nofollow noreferrer">RealTerm</a> (for Windows, if that's your thing) which allows for a wide array of serial port nitty-gritty details.</p>
