Pages: [1]   Go Down
Author Topic: String to LongHex  (Read 562 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

first of all, sorry for my bad english. I'm new to the arduino development, so I got a simple question. I got to control some LEDs with the Arduino Board, and it works fine, now i want to set the Color from an extern source so I need  to Convert a String like "FFFFFF" (which i get from a client as a String) to a long hex number (0xFFFFFF). Its a simple problem, but i just cant solve it. I looked for solutions with atoi() but it doesnt work.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to show your code, so we can see if you are talking about a string or a String. You shouldn't be using String; it has a number of issues.

The atoi() function is a(scii)toi(nt). The strtol() function is string to long, which is what you want.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long color = 0;

void loop()
{
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    String clientMsg ="";
    String colorString = "";
    String positionString = "";
    while (client.connected()) {
      if (client.available()) {
       client.println("Hi there!");
       char c = client.read();
       clientMsg+=c;//store the recieved chracters in a string
       //if the character is an "\" the whole message is recieved
       if (c == '\\') {  

       colorString = colorString + clientMsg.charAt(1) + clientMsg.charAt(2) + clientMsg.charAt(3) + clientMsg.charAt(4) + clientMsg.charAt(5) + clientMsg.charAt(6); // create the hex color String
       color = stringToNumber(colorString);


....

  int stringToNumber(String thisString) {
  int i, value = 0, length;
  length = thisString.length();
  for(i=0; i<length; i++) {
    value = (10*value) + thisString.charAt(i)-(int) '0';;
  }
  return value;
}

the stringToNumber doesnt work, i found it on another forum. I need a replacement for this function. I tried some other solutions but i also didnt work. I also tried the strtol, but the long is still 0.

Code:
char str[6] = { clientMsg.charAt(1) , clientMsg.charAt(2) , clientMsg.charAt(3) , clientMsg.charAt(4) , clientMsg.charAt(5) , clientMsg.charAt(6) };
       color = strtol(str, NULL, 6);
« Last Edit: December 05, 2012, 08:06:30 am by Kelthur » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you using Strings? You are going to have problems until you stop.

You're accessing different sets of characters in the two methods - 1 to 6 in one case and 0 to n in the other. You need to make up your mind which set is important.

Finally, the code you posted does something. You want it to do something. All you have said about those two somethings is that the second one is not what you want. What that is, or what the code actually does is a mystery.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're accessing different sets of characters in the two methods - 1 to 6 in one case and 0 to n in the other. You need to make up your mind which set is important.

where? clientMsg is the same set or i missunderstand something

what i want exactly is to convert my for Example "FFFFFF" string into a 0xFFFFFF long... somehow
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 49998
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
where? clientMsg is the same set or i missunderstand something
Never mind, I was reading it wrong. I still don't understand why you skip the first character in clientMsg, though.

I still don't know what is in client message, nor do I know what you get when you call strtol().

The code in stringToNumber is clearly designed to handle base 10 strings, only. You have to multiply by 16 each time, if you know the string is a string of hex characters, and 'F' - '0' is not 15.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get a String with several Settings für a LED-strip, the String contains in the first Character what scenario it is (blinking LED, running LED etc.) in the 1-6 Characters I get the Color for the RGB-LED-strip, thats why i need to pull apart the String. The color of every LED is stored in a long-array. Every long is a color of 1 LED of the strip. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

solved the Problem with

Code:

long color = 0;
char str[6] = { clientMsg.charAt(1) , clientMsg.charAt(2) , clientMsg.charAt(3) , clientMsg.charAt(4) , clientMsg.charAt(5) , clientMsg.charAt(6) };

Code:
sscanf(str,"%lX", &color);
Logged

Pages: [1]   Go Up
Jump to: