title: Adding leading zeros to a Hex number within bash script
body: <p>I'm working on a CAN bus project, and trying to send a message to set the time and date. I've worked out how the message needs to be formatted, grabbed the date/time and stored in variables. I've converted them to hex just fine, but I need to add leading 0s to pad to the required space required by the message.</p>
  
  <p>I've tried methods seen online for a bash script, but I have a problem:
  The year 2019 is 7E3 in hex. I need this displayed as 07E3. When using awk to add the leading 0s, the E3 is being interpreted as Engineering notation *10^3, and therefore printing as 7000. My script is below, along with an image showing the CAN bus message format. Any help is appreciated.</p>
  
  <pre><code>#!/bin/bash
  
  #Store Date/Time in Variables
  Year=`date '+%Y'`
  Month=`date '+%m'`
  Day=`date '+%d'`
  Hour=`date '+%H'`
  Minute=`date '+%M'`
  Second=`date '+%S'`
  
  #Convert Date/Time to Hexadecimel
  HexYear=`echo "ibase=10;obase=16;$Year"| bc | awk '{ printf("%04d\n", $1) }'`
  HexMonth=`echo "ibase=10;obase=16;$Month"| bc | awk '{ printf("%02d\n", $1) }'`
  HexDay=`echo "ibase=10;obase=16;$Day"| bc | awk '{ printf("%02d\n", $1) }'`
  HexHour=`echo "ibase=10;obase=16;$Hour"| bc | awk '{ printf("%02d\n", $1) }'`
  HexMinute=`echo "ibase=10;obase=16;$Minute"| bc | awk '{ printf("%02d\n", $1) }'`
  HexSecond=`echo "ibase=10;obase=16;$Second"| bc | awk '{ printf("%02d\n", $1) }'`
  
  echo "The following is Decimel &gt; Hex"
  echo "$Year &gt; $HexYear"
  echo "$Month &gt; $HexMonth"
  echo "$Day &gt; $HexDay"
  echo "$Hour &gt; $HexHour"
  echo "$Minute &gt; $HexMinute"
  echo "$Second &gt; $HexSecond"
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/cxkD7.png" rel="nofollow noreferrer">CAN bus message format</a></p>