Converting Decimal To HEX Values In Arduino
Maybe your like
Wednesday, 6 May 2015
converting Decimal to HEX values in Arduino
while i am searching for a way to send the HEX values rather than Int / char values using Arduino Serial communication. i find that Serial.print() will do the work for me.void setup(){int a = 17;Serial.begin(9600);Serial.print("value of a :"); Serial.println(a);Serial.print("HEX equalent of a :"); Serial.println(a,HEX);}void loop(){//do nothing}and the output is like
it is good enough to do the work for me But there is a chance of error if i wanted to send a value in the middle of array.the problem arise when we wanted to print leading zeros for fixed two byte and 4 byte HEX valuesif we wanted to print values 1 and 1000,we get hex equivalents as
which is printed by omitting zero's. and doesn't specify the no of HEX digits in outputs so serial.print(var,HEX) won't print the HEX values with Leading zeros. we need to find a way to do it.In C there is a function called sprintf() which will be the saviour of our day.It is used to format a string which is used to print at the outputthe syntax of sprintf issprintf(char *str,const char* format)But first we wanted to print hex values so we need to know the different HEX format specifiers.we can simply know by compiling and running this simple code#include int main() { int data = 29; printf("%x\n", data); // just print data printf("%0x\n", data); // just print data ('0' on its own has no effect) printf("%8x\n", data); // print in 8 width and pad with blank spaces printf("%08x\n", data); // print in 8 width and pad with 0's return 0;and the result is
by using both format specifiers and sprintf we can print the hex value as it isvoid setup(){Serial.begin(9600);int a = 0x0094; Serial.println(a); Serial.print("serialprint using HEx : "); Serial.println(a,HEX); print_hex(a);}void loop(){ }void print_hex(int val){ char str[20];sprintf(str,"%04x",val);Serial.print("serialprint using print_hex function : "); Serial.println(str);}//end of printhex() functionand the output is
But what if we wanted to convert a decimal value to HEX and sent it as HEX?we can use the below code to do that./*Author : Kunchala Anilconverting Decimal value into HEX */#define max_hex_length 4char data_buffer[20];int hex[4];void setup(){Serial.begin(9600); }//end of main()void loop(){ int a = serial_ask(); Serial.print("the value you entered is :"); Serial.println(a); convert_to_hex(a); }//end of loopvoid convert_to_hex(int a){ for (int i=0; i = 0; j--){ if (hex[j] <= 9){ Serial.print(hex[j]); } else{ switch(hex[j]){ case 10 : Serial.print('a'); break; case 11 : Serial.print('b'); break; case 12 : Serial.print('c'); break; case 13 : Serial.print('d'); break; case 14 : Serial.print('e'); break; case 15 : Serial.print('f'); break; default : Serial.println("something wrong"); }//end of switch }//end of If Else Condition }//end of for loop Serial.println();}//end of convert_to_hex() functionint serial_ask(){ Serial.println("enter the Integer decimal value coverted to the HEX"); while(!data_received()){ //wait until full data is received } return(atoi(data_buffer));}//end of serial_ask() fucntionboolean data_received(){ while(!Serial.available()){ //wait until user enters the data } if(Serial.available()){ static byte index = 0; char input = Serial.read(); if(input != '\r'){ data_buffer[index] = input; index ++; } else { data_buffer[index] = 0;//terminating with NULL to make char array as string index = 0; return true; }//end of If Else condition }//end of IF condition return false;}//end of data_received() functionand the output is
Labels: arduino, arduino decimal to hex, decimal to hex conversion, sprintf in arduino No comments:
Post a Comment
Newer Post Older Post Home Subscribe to: Post Comments (Atom)Google Profile
Kunchala Anil View my complete profileAbout Me
Hi, My Name is Kunchala Anil. I completed my masters recently. In this blog i try to share my limited knowledge about the stuff i regularly tinker like Arduino, Raspberry Pi, Nodemcu, Python , Matlab ..etc. Pleas feel free to share any feedback on content via anilkunchalaeceATgmailDoTcomBlog Archive
- ► 2021 (2)
- ► June (2)
- ► 2020 (1)
- ► January (1)
- ► 2019 (2)
- ► February (2)
- ► 2018 (6)
- ► September (1)
- ► May (2)
- ► March (3)
- ► 2017 (1)
- ► July (1)
- ► 2016 (18)
- ► November (1)
- ► September (1)
- ► August (2)
- ► July (2)
- ► June (1)
- ► May (1)
- ► April (1)
- ► March (1)
- ► February (5)
- ► January (3)
- ► 2014 (10)
- ► December (6)
- ► November (4)
Search This Blog
Report Abuse
-
I see you - lincoln exhibition Ok.... How should I start this ? I volunteered frequency festival taking place in Lincoln, England. They put me in charge of exhibit c... - Piezo electric power generation in tires Long ago I wrote a small paper on "Piezo electric power generation in tires" It seems that I can't find any working link for ...
-
Playing with Strings, HEX and Integer values. In the past few days I have tough time converting Inter values into Hex values and how to store them in strings. If once i stored them How t...
Tag » Arduino Convert Decimal A Hexadecimal
-
How Do I Convert Decimal To Hexadecimal? - Arduino Forum
-
Decimal To Hexadecimal - Store In A String - Arduino Forum
-
Converting Decimal To Hex - Programming Questions - Arduino Forum
-
Convert Integer/decimal To Hex On An Arduino? - Stack Overflow
-
Arduino Dec To Hex Code Example - Code Grepper
-
Convert Integer/decimal To Hex On An Arduino? - Tech Notes Help
-
Arduino-Hex-Decimal-Conversion/hex_o At Master - GitHub
-
Convertir Entier/nombre Décimal En Hexadécimal Sur Un Arduino?
-
Convert Hex To Decimal From Signed 2's Complement
-
Resolved: Arduino - How To Convert Double To HEX Format
-
How To Write Arduino Code To Convert An 8-bit Binary To A Decimal ...
-
Print Hexadecimal Values In Arduino - Tutorialspoint
-
Creating A Number System Converter Create A Program