-1

Please explain in detail. How the Hexadecimal code has been assigned?

Edit: This is the code:

#include <Wire.h> 
void setup() 
{
   Wire.begin(); 
   Serial.begin(9600); 
   delay(1000); 
   Serial.println("AT+CMGF=1"); //set to SMS mode
   delay(1000);
   Serial.println("AT+CMGS=\"+918460588440\""); //set the phone number (wrapped in double quotes)
   delay(1000);
   Serial.println("hello world. this is arduino");
   delay(1000);
   Serial.write(0x1A); // sends ctrl+z end of message
   delay(1000);
   Serial.write(0x0D); // Carriage Return in Hex
   delay(1000);
   Serial.write(0x0A);
}
void loop() {}
  • 2
    Please don't ask the same question twice. – Joris Groosman May 17 '15 at 11:04
1

They are all to do with where in the alphabet they are. There are 26 letters in the English alphabet, and Z is the 26th. The 26 in hexadecimal is 0x1A.

A carriage return is a CTRL-M, and a line feed is a CTRL-J. M is the 13th letter of the alphabet, and 13 in hexadecimal is 0x0D. J is the 10th letter - 10 is 0x0A in hexadecimal.

Why are they CTRL-M and CTRL-J? Well, that all stems from the old teletype interfaces used on the very early computers. That's what they used, and that's what's stuck, and finally made it into the ASCII standard.

  • Why are these lines added at the end of arduino code? – user4236 May 17 '15 at 11:53
  • Because they are. Whatever "the code" is, it obviously requires them. Without knowing what "the code" is I can't comment any further. – Majenko May 17 '15 at 11:58
  • this is the code <pre> #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); delay(1000); Serial.println("AT+CMGF=1"); //set to SMS mode delay(1000); Serial.println("AT+CMGS=\"+918460588440\""); //set the phone number (wrapped in double quotes) delay(1000); Serial.println("hello world. this is arduino"); delay(1000); Serial.write(0x1A); // sends ctrl+z end of message delay(1000); Serial.write(0x0D); // Carriage Return in Hex delay(1000); Serial.write(0x0A); } void loop() {} – user4236 May 17 '15 at 11:59
  • No, that is a huge mess of text that I can't decypher. My guess is that whatever you are talking to requires those characters as part of its protocol. – Majenko May 17 '15 at 12:00
0

These hex values are the numbers in the ASCII table, https://en.wikipedia.org/wiki/ASCII.

Characters corresponding to values below 32 are control characters, reserved for special purposes. For the values you mention in the question:

  • 0x0d = 1*16 + d*1 = 13 = CR (carriage return)
  • 0x0a = 0*16 + a*1 = 10 = LF (line feed)
  • 0x1a = 1*16 + a*1 = 26 = SUB

("control+Z" is not the ASCII control character, but how you would generate this control character (SUB) on a keyboard.)

A table with both hexadecimal and decimal numbers is also given in the Wikipedia article.

0

(I've formatted your code and edited it into your question).

This code is apparently driving a device - GPRS shield? - to send an SMS. No doubt the code was written to meet the requirements of the device. Note that the CR and LF literals could have been written as '\r' and '\n', respectively. The author chose to write them in hex, probably because there is no "\" code defined for Ctrl-Z, and so wrote all three in a consistent style.

Your Answer

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