1

hello there my code is here.

/* Simple Serial ECHO script : Written by ScottC 03/07/2012 */
/* Use a variable called byteRead to temporarily store
 the data coming from the computer */
byte byteRead;
void setup() { 
// Turn the Serial Protocol ON
 Serial.begin(9600);
}
void loop() {
 char a[8];
 int b;
 /* check if data has been sent from the computer: */
 if (Serial.available()) 
 {
 /* read the most recent byte */
 byteRead = Serial.read();
 Serial.print(byteRead,HEX);
 }

now when i send '12' from terminal, it prints 3132. means it is ASCII value. but i want to print 12. can you help me?? how to do it??

asked Apr 4, 2017 at 9:26
9
  • You need to convert the ASCII characters to a number. Commented Apr 4, 2017 at 9:39
  • how?? can i use atoi ?? Commented Apr 4, 2017 at 9:41
  • how about strtol? is allows a base. Commented Apr 4, 2017 at 9:43
  • i use it this way. but i am getting compiler error..... Commented Apr 4, 2017 at 9:57
  • ret = strtol(byteRead, &ptr, 10); Commented Apr 4, 2017 at 9:57

2 Answers 2

4

There are several issues with your sketch. The most important is the lack of understanding for the difference between internal and external representation.

The sketch reads a single character from Serial and prints the value as a hexadecimal number. The character is ASCII; a number that represents symbols in our alphabet.

Your goal is to read characters into a string and then convert the string to a number assuming that the string represents a hexadecimal number ('0'..'9' and 'a'..'f' or 'A'..'F').

The sketch should be 1) scan string, 2) parse and convert string to number, 3) print the number.

Scanning the string requires skipping whitespace and then collecting characters until a whitespace.

The sketch could look something like this:

void setup() {
 Serial.begin(57600);
 while (!Serial);
 Serial.println(F("Serial started..."));
}
void loop() {
 const size_t BUF_MAX = 16;
 char buf[BUF_MAX];
 char* bp = buf;
 char c;
 // Skip whitespace
 do {
 while (!Serial.available());
 c = Serial.read();
 } while (c <= ' ');
 // Scan token until whitespace
 *bp = c;
 do {
 while (!Serial.available());
 c = Serial.read();
 *++bp = c;
 } while (c > ' ');
 *bp = 0;
 // Print scanned token
 Serial.println(buf);
 // Convert to number and print
 const int BASE = 16;
 long val = strtol(buf, &bp, BASE);
 if (*bp == 0)
 Serial.println(val);
 else
 Serial.println(F("not a number"));
}

This allows scan of hexadecimal numbers with possible sign (as defined by strtol). By changing the BASE to zero strtol will use the notations for base definitions (prefix '0' for octal, '0x' for hexadecimal). Last, please observe the parse error handling provided by strtol(*bp != 0).

Cheers!

answered Apr 4, 2017 at 14:55
0
3

To convert Decimal or Integer value to Hexadecimal you can go this way..

byteRead = Serial.read(); //Read your value
myValue = String(byteRead, HEX); //Convert it into Hexadecimal 
Serial.println(myValue); //Print in serial Monitor 
myValue1 = String(byteRead,DEC); //Convert it ino Decimal
Serial.println(myValue1); //Print in serial Monitor 
answered Apr 4, 2017 at 9:43
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 5, 2017 at 8:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.