Author Topic: Parse a string in AVRstudio  (Read 4730 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Parse a string in AVRstudio
« on: January 14, 2009, 09:30:10 PM »
I want to write a bit of code that converts a string of hex-RGB-representation - that thing in website you set to FF6575, or whatever. These : http://www.w3schools.com/CSS/css_colors.asp.

I need some way to parse or filter out certain parts of the string. For example I need to separate it into three groups : FF 65 75.
The only way that I can think of doing it is to transfer the strings into an array of bytes(after all a string is just a bunch of bytes), and then just call each byte one by one based on its "position in line" in the array.

What do you guys suggest?
Thanks
,Eric
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline yerbie

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 3
Re: Parse a string in AVRstudio
« Reply #1 on: January 14, 2009, 09:58:44 PM »
you can do something like this...
Code: [Select]
#include <stdio.h>
#include <string.h>

int main(void) {
  char str[] = "FF6544";
  char hex1[3], hex2[3], hex3[3];
  strncpy(hex1, str,2);
  strncpy(hex2, &str[2],2);
  strncpy(hex3, &str[4],2);
  hex1[2] = '\0';
  hex2[2] = '\0';
  hex3[2] = '\0';
  printf("hex1:%s\n",hex1);
  printf("hex2:%s\n",hex2);
  printf("hex3:%s\n",hex3);
  return 0;
}

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Parse a string in AVRstudio
« Reply #2 on: January 14, 2009, 10:44:51 PM »
you can do something like this...
Code: [Select]
#include <stdio.h>
#include <string.h>

int main(void) {
  char str[] = "FF6544";
  char hex1[3], hex2[3], hex3[3];
  strncpy(hex1, str,2);
  strncpy(hex2, &str[2],2);
  strncpy(hex3, &str[4],2);
  hex1[2] = '\0';
  hex2[2] = '\0';
  hex3[2] = '\0';
  printf("hex1:%s\n",hex1);
  printf("hex2:%s\n",hex2);
  printf("hex3:%s\n",hex3);
  return 0;
}

The output of that gives me
Quote
hex1:s
hex2:s
hex3:s
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
Re: Parse a string in AVRstudio
« Reply #3 on: January 14, 2009, 10:53:06 PM »
look into scanf. it works like printf, but for input.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Parse a string in AVRstudio
« Reply #4 on: January 14, 2009, 11:06:21 PM »
too little code to look at for reference. Anyways I decided to just do this
SetRGB(FF,65,75);

which calls the SetRGB routine which looks like this
Code: [Select]
void SetRGB(red,green,blue);
{
// do stuff with the contents of red, green, blue
}
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline bryan922

  • Jr. Member
  • **
  • Posts: 25
  • Helpful? 0
Re: Parse a string in AVRstudio
« Reply #5 on: January 15, 2009, 12:34:43 AM »
too little code to look at for reference. Anyways I decided to just do this
SetRGB(FF,65,75);

Why would that work? If you want to use hexadecimal digits as values, then precede them with a 0x like this: 0xff, 0x65, 0x75.

Of course, FF is really just 255, so if you can think in terms of 0-255, then just use values like that.

If you need to parse something like "FF", then do something like this:

// assuming we already have hex...
hex[3] = "FF";

// then...

unsigned char = ( hex[0] - 'A' ) * 16 + hex[1] - 'A';

And what that means is...

Subtract the ascii equivalent of 'A' from the ascii equivalent of 'F', which yields 15. Multiply that by 16, which yields 240, and do the same for the second F, but don't multiply it by 16, so we get 15, and we add that to 240, which yields 255...
« Last Edit: January 15, 2009, 12:35:36 AM by bryan922 »

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Parse a string in AVRstudio
« Reply #6 on: January 15, 2009, 03:31:36 AM »
I want to write a bit of code that converts a string of hex-RGB-representation - that thing in website you set to FF6575, or whatever. These : http://www.w3schools.com/CSS/css_colors.asp.
I need some way to parse or filter out certain parts of the string. For example I need to separate it into three groups : FF 65 75.


Where do you get the text representation from and what other limitations are there? Parsing this kind of text is real easy and can be done with minimum of code (in C - you're using C, right?) but there are two ways to do it: You can have a function that takes as a parameter an pointer to a string, parses that string and stores the results in global variables OR you can have a function that's called with one CHAR parameter with each char as it becomes available.

Sample: You'll use this kind of function if you have the text in memory and just want to parse it:
Code: [Select]
void parsetext(char*) {
}

int main(void) {
  parsetext("#A1B2C3"); // Parse result is stored in Red, Blue and Green global variables
  SetRedComponentOfLed(Red);
  SetBlueComponentOfLed(Blue);
  SetGreeenComponentOfLed(Green);
}


Sample 2: You'll use this kind of function if you're getting the chars off some kind of UART port so you don't need to store them.
Code: [Select]
int parsenextchar(char) {
}

void GotAnCharFromUART(char thechar) {
  int parseres = parsenextchar(thechar); // Parses the next char received from UART. If it completed reading an RGB color it will
  // return an code (letting us know we got a color) and sotres the R, G and B components in global variables   

  if (parseres == 1) {
    // Got the full RGB code parsed!
    SetRedComponentOfLed(Red);
    SetBlueComponentOfLed(Blue);
    SetGreeenComponentOfLed(Green);
  }
}

int main(void) {
}


The implementation details for both functions would be similar: I'd implement a finite state machine (the kind of code LEX generates). I'd do it by hand since I've done it many times before. This kind of code is incredible effective and very fast. If you're interested I could wirte any of those two tonight when I get home from work.

 


Get Your Ad Here