How to interpreter results diferently?

Hy there…

So here"s the thing I need to build some kinda simple calculator where my input from serial is HEX
but output Serial.write or print is in ASCII…

I can easily do oposit but got some problem with this way…
first the HEX is usually 2 characters which is equal to one of ASCII for example HEX 31 = 1 ASCII
HEX 46 = F ASCII

just because of that your input has to be an array or string like I guess, right???
or perceived by Arduino as HEX… I defanatly don’t know how to do it couse it’s seems like everything that arduino gets from serial is always INTs and in singles… then you interpreter it the way you want it right???

Basicaly from 2 chars I got to get one and then interpreter it in Serial back with Serial.write

So it’s got be some how like that

int Str2[0] + Str[1] = Serial.write(result, ASCII) // one character

  • but not the plus but what???

So here is my code can some one add the needs pls…

int thisByte1 = 33;
char Str2[2];
void setup()
{
Serial.begin(57600);
Serial.println(“Start”);

}

void loop()
{
if(Serial.available()>0)
{
for (int i=0;i<2;i++) Str2*=0; //возрат на ноль нах*

  • Serial.readBytes(Str2,2);*
  • int i;*
  • for(i=0; i<2; i=i+1)*
  • {*
    _ Serial.write(Str2 );_
    * }*
    * Serial.println(" ");*
    * //Serial.write(Str2);*
    * }*
    }[/quote]
    Yep the array needs to be in ints from them it’s actually easy to convert but the function readBytes doesn’t wanna work even with bytes… how to read in to array diferent way for me is problem too…
    That’s way Im here

An approach: http://forum.arduino.cc/index.php?topic=45166.0

Ray

To convert a pair of characters to a single number, multiply the first value by 16 and add the second value.

You don’t necessarily need an array. You could simply process each character as you receive it.

Like so (totally untested but it should give you the general idea)

void setup()
{
Serial.begin(9600); 
}

int currentEntry=0;
int runningTotal=0;
byte currentOperation='+';

void loop()
{
char c;
if(Serial.available())
  {
   c=Serial.read();
   Serial.print(c);
   
   if (c >='0' && c <='9')
      {
       currentEntry *=16;
       currentEntry += c-'0';
      }
 
   if (c >='a' && c <='f')
      {
       currentEntry *=16;
       currentEntry += 10 + c-'a';
      }
 
   if (c >='A' && c <='F')
      {
       currentEntry *=16;
       currentEntry += 10 + c-'A';
      }
  if ((c=='+') || (c=='-') || (c=='*')|| (c=='/') || (c=='=') )
    {
      switch (currentOperation)
      {
       case '+':
         runningTotal += currentEntry;
         break;
       case '-':
         runningTotal -= currentEntry;
         break;
       case '*':
         runningTotal *= currentEntry;
         break;
       case '/':
         runningTotal /= currentEntry;
         break;       
      }
     currentOperation=c;
     currentEntry=0;
     if(c=='=')
       {Serial.println(runningTotal,HEX);
        runningTotal=0;
        currentOperation='+';
       }
    } 
  }
}

int thisByte1 = 33;
char Str2[2]; //initialize

byte Result;
void setup()
{
Serial.begin(57600);
Serial.println(“Start”);

}

void loop()
{
if(Serial.available()>0)
{
for (int i=0;i<2;i++) Str2*=0; *

  • Serial.readBytes(Str2, 2); *
    // stream.readBytes(buffer, length)

_ Result = ((Str2[0]*16) + Str2[1])-48;_

  • int i;*
  • for(i=0; i<2; i=i+1)*
  • {*
    _ Serial.write(Str2*); _
    _
    }_
    _
    Serial.println(" ");*_

* Serial.write(Result);*
* Serial.println(" ");*

* }*
}[/quote]
Done!!.. As simple as always at the end…

KenF: You don't necessarily need an array. You could simply process each character as you receive it.

Hay man your code is SOoo advanced!!!

mrburnette: An approach: http://forum.arduino.cc/index.php?topic=45166.0

Ray

Thanks... But it would be better if you point it out next time more closely. Just because I sow this post before

Hello again guys...

in continue of this topic... I just realize that it's actually harsh to convert just opposite!!!

So what I did above is converting from HEX to ASCII from Serial to Serial by this formula :

DEC = HEX1*16+1 as long as I use arrays...

But if you want convert it from DEC to HEX just opposite another problems come... now the arrays has to be 3 char cause the max passable in HEX is FF which is 255 in DEC..

And the formula doesn't work because it's 2 missing arguments now...

So from 3 difrent separate chars in ASCII to 2 HEX in serial... How?

What you have to get your head around is that ascii and hex are just to do with display. A value is a value, no matter how you express it.

If you have an integer value then it's just a value. You can express that value in binary, octal, decimal, hex or even to the base of three if you want. But the value is just a value, it isn't intrinsically hex or ascii.

so in your post above DEC = HEX1 blah blah blah should read VALUE = HEX1 blah blah blah.

The value you now have isn't decimal, hex or any other base, it's just a value being stored in a variable.

Picture yourself lying on a beach in some tropical location. Imagine a palm tree casting its shade upon you. Would this still be possible if you were French? The sky would look the same, the leaves of that palm tree would look the same. Even though, when I ask you to describe the scene you'd mention a blue sky and green palm fronds. and a Frenchman would use completely different terms to describe the same thing.

With values held within variables, they are just values. It's only when you want to express them that the language you choose makes any difference.

I know it and understand it… That it doesn’t mette what is it… metters is value you represent it as you want it…

The F problem is that F arduino that 255 treat as 2 5 5 that’s where I have problem always needed some kind of formula

So how the F read it as single char

String Str = Serial.ReadString();

int Ch = Str; - can’t do that…

OK so now consider this. You can have bytes and you can have chars. Consider this very simple piece of code

byte dataVariable=0;
char character='0';

You now have two variables dataVariable holds the value of 0, whereas character holds the value of 48 (the character code of "0")

Those apostrophes around the 0 tell the compiler that you don't mean zero, you mean the character code of "0"

So if you have a character "0" turn up in your serial interface, you can convert it back to zero by simply subtracting '0' (note the apostrophes again) This will then convert that nasty 48 back to 0

In fact this will work for all the numerical characters, just subtract '0' from the character and you get a value equivalent to what that digit represents.

If you've already recieved other characters, you have to multiply the value you have so far by 10 before adding the current character.

So looking at that string of 2 5 5

First you deduct '0' from the "2" and you get a value of 2. You now have 2

Next you find a character "5" deduct '0' from it and you get a value of 5 multiply the 2 you already have by 10 and add on the 5 you just got You now have 25

Next you find another character "5" deduct '0' from it and you get a value of 5 multiply the 25 you already have by 10 and add on the 5 you just got You now have 255

You then find you're at the end of the string so your conversion is complete

that's it... that's the way, that's the formula...

Where is it says that... (I didn't know that...ofcuorse I could not think of that)

Thanks

Slavka85: that's it... that's the way, that's the formula...

Where is it says that... (I didn't know that...ofcuorse I could not think of that)

Thanks

If you now have another look at the example code I gave above. You'll realise that it's not " SOoo advanced" after all :)

String Str;

void setup() { Serial.begin(57600); Serial.println("Start"); } void loop() { if(Serial.available()>0) { Str = Serial.readString();

int result = (((Str[0]-48)*10+(Str[1]-48))*10)+(Str[2]-48);

Serial.println(result); } }

So what is it if it's not the formula?

String Str;

int result;

void setup()
{
  Serial.begin(57600);
  Serial.println("Start");
}
void loop()
{
  if(Serial.available()>0)
  {
        Str  = Serial.readString();
        int ch1 = Str.length();
        
        if(ch1 == 1)
        {
          result = Str[0]-48;
        }
         
          if(ch1 ==2)
        {
          result = ((Str[0]-48)*10)+Str[1]-48;
        } 
          
          if(ch1 ==3)
        {
           result = (((Str[0]-48)*10+(Str[1]-48))*10)+(Str[2]-48);
        } 
      
     
      
      Serial.println(result);
      Serial.println(result, HEX);
      Serial.println(" ");
     

  } 
}

IS there anyway to simplify that???

works with digits well like KenF said... Don't get it what to do with Letters like A a B b etc

Also HA how about that:

String 185

int (185) = 1*100+8*10+5;

I guess it can be more logical.. ha