Go Down

Topic: String to LongHex (Read 609 times) previous topic - next topic

Kelthur

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.

PaulS

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.

Kelthur

#2
Dec 05, 2012, 01:58 pm Last Edit: Dec 05, 2012, 02:06 pm by Kelthur Reason: 1
Code: [Select]
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: [Select]
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);

PaulS

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.

Kelthur


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

PaulS

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.

Kelthur

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. 

Kelthur

solved the Problem with

Code: [Select]

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: [Select]
sscanf(str,"%lX", &color);

Go Up