Advertisement
Guest User

String Hex to long converter.

a guest
Feb 25th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1.  
  2. const unsigned int MAX_INPUT = 50;
  3. String Line[10];
  4.  
  5. void setup() {
  6.  
  7.   Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.  
  12.   //check for terminal input via display.
  13.  
  14.  
  15.   while (Serial.available () > 0) {
  16.  
  17.     processIncomingByte (Serial.read ());
  18.  
  19.   }
  20.  
  21.  
  22. }
  23.  
  24.  
  25. void processIncomingByte (const byte inByte)
  26. {
  27.   static char input_line [MAX_INPUT];
  28.   static unsigned int input_pos = 0;
  29.  
  30.   switch (inByte)
  31.   {
  32.  
  33.     case '\n':   // end of text
  34.       input_line [input_pos] = 0;  // terminating null byte
  35.  
  36.       // terminator reached! process input_line here ...
  37.       process_data (input_line);
  38.  
  39.       // reset buffer for next time
  40.       input_pos = 0;
  41.       break;
  42.  
  43.     case '\r':   // discard carriage return
  44.       break;
  45.  
  46.     default:
  47.       // keep adding if not full ... allow for terminating null byte
  48.       if (input_pos < (MAX_INPUT - 1))
  49.         input_line [input_pos++] = inByte;
  50.       break;
  51.  
  52.   }  // end of switch
  53.  
  54. }
  55.  
  56.  
  57. void process_data(char str[])
  58. {
  59.   char *pch;
  60.  
  61.   //text parsing
  62.   int counter = 0;
  63.   pch = strtok(str, " ,.-");
  64.  
  65.   //read through the input and split it into new lines based on where spaces and other symbols are.
  66.   while (pch != NULL)
  67.   {
  68.     //set line to be equal to the split.
  69.     Line[counter] = pch;
  70.  
  71.     pch = strtok(NULL, "= ,.>");
  72.     counter++;
  73.   }
  74.   Serial.println(Line[0]);
  75.   Serial.println(Line[1]);
  76.  
  77.  
  78.   //if the command is the one we are looking for then run the hex command
  79.   if (Line[0] == "HEX") {
  80.  
  81.  
  82.     ConvertHex(Line[1]);
  83.  
  84.   }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. }
  91.  
  92. long ConvertHex(String input)
  93. {
  94.  
  95.   //how many hex chars there are. (if you change this youll need to redo the math at the bottom.
  96.   int HexValues[4];
  97.  
  98.   //print how many get recived.
  99.   Serial.println(input.length());
  100.  //for the ammount of hex chars run through them and convert them into their individual base 10 values.
  101.   for (int i = 0; i <= input.length() - 1; i++)
  102.   {
  103.  
  104.     switch (input.charAt(i))
  105.     {
  106.       case 'A':
  107.         HexValues[i] = 10;
  108.         break;
  109.  
  110.       case 'B':
  111.         HexValues[i] = 11;
  112.         break;
  113.  
  114.       case 'C':
  115.         HexValues[i] = 12;
  116.         break;
  117.  
  118.       case 'D':
  119.         HexValues[i] = 13;
  120.         break;
  121.  
  122.       case 'E':
  123.         HexValues[i] = 14;
  124.         break;
  125.  
  126.       case 'F':
  127.         HexValues[i] = 15;
  128.         break;
  129.  
  130.       default:
  131.         //if value is equal to its base 10 counter part then leave it be and convert it to int.
  132.         HexValues[i] = (input.charAt(i) - '0');
  133.         break;
  134.     }
  135.  
  136.     //print the value of each hex symbol on its own.
  137.     Serial.print("value:");
  138.     Serial.println(HexValues[i]);
  139.   }
  140.  
  141.  
  142.   // add up all of the values and multiply based of their current place and to convert them to base 10.
  143.   Serial.print("Total value:");
  144.   Serial.println(HexValues[0] * 4096 + HexValues[1] * 256 + HexValues[2] * 16 + HexValues[3]);
  145.   return (HexValues[0] * 4096 + HexValues[1] * 256 + HexValues[2] * 16 + HexValues[3]);
  146.   //if you want to change this to do 24 bit hex inputs youll need to redo the math. here is where i got the equation from.
  147.   //https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=1111
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement