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...
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
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.
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