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??

  • You need to convert the ASCII characters to a number. – Mikael Patel Apr 4 '17 at 9:39
  • how?? can i use atoi ?? – Beginner Apr 4 '17 at 9:41
  • how about strtol? is allows a base. – Mikael Patel Apr 4 '17 at 9:43
  • i use it this way. but i am getting compiler error..... – Beginner Apr 4 '17 at 9:57
  • ret = strtol(byteRead, &ptr, 10); – Beginner Apr 4 '17 at 9:57
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!

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.