Converting Decimal To HEX Values In Arduino

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 likehex equait 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 asleading zero problemwhich 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 isHEX format specifierby 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 isprint_hex functionBut 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 isdec to hex

No comments:

Post a Comment

Newer Post Older Post Home Subscribe to: Post Comments (Atom)

Google Profile

Kunchala Anil View my complete profile

About 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 anilkunchalaeceATgmailDoTcom

Blog Archive

  • ▼  2015 (60)
    • ▼  May (18)
      • Seven Segment LED display with Arduino and Proteus
      • size of Array Vs No of elements in Array
      • Interfacing Keypad with Arduino
      • Two Arduino's Communication with USART
      • Proteus Virtual Terminal using Arduino Hardware an...
      • Arduino Serial communication in proteus using Virt...
      • Arduino Relay driver using optocoupler in Proteus
      • Adding Arduino Library to proteus 8
      • Arduino Proteus8 turorial
      • Using relay with Arduino
      • VFD & arduino
      • controlling VFD with arduino...
      • Arduino & load cell
      • Finding a Two's complement of a number
      • LRC calculation for MODBUS ASCII Protocol In Arduino
      • Playing with Strings, HEX and Integer values.
      • converting int values into HEX and storing it in a...
      • converting Decimal to HEX values in Arduino

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