Convert Hex/Binary/DEC to signed char

As title says, is there a way to convert a byte of data to a signed char so I can get the - numbers. I am using an Xbee to transmit data and I can only pass 1 byte of data. (I can convert that data to DEC, HEX or BIN) that data is compiled as a signed char because I am using an acceleramator sensor.

I don't understand this question.

For the Arduino, a byte is unsigned 8-bit number.
http://www.arduino.cc/en/Reference/Byte

For the Arduino, a char is 1 byte of memory.
https://www.arduino.cc/en/Reference/Char

byte and signed char are only different interpretations of 8 bits.

You can influence the interpretation of the compiler by defines, casts, ...

Better show your code and tell us what you want to achieve.

Here is a code that I parse the data of my payload, save it to a String and add it to a string called “storedData”.

I want to take rx.getData() and get the signed char value, the -128 to 127 values.
I tried doing signed char b = rx.getData();
Then saying String a = b; or String a = string(b);
Both of those options give me garbage characters…
*_ <em>*  Serial.print("payload [");           Serial.print(i, DEC);           Serial.print("] is ");           Serial.println(rx.getData()[i]);           String a = String(rx.getData()[i]);           storedData.concat("." + a );*</em> _*

Without definition of rx.getData that code is pretty meaningless.
It seems to return a pointer, but a pointer to?

What is the output? Or are there error messages?

With the code as is, I get values of 1-255. Basically a byte return.

rx.getData() is from the xBee library

http://xbee-arduino.googlecode.com/svn/trunk/docs/api/class_rx_data_response.html#e0f858fe479a07c7122a8d414c60517e

You can cast; that tells the compiler to treat the byte as a character.

Below code prints the content of byte b in hex, unsigned decimal (as it's a byte), signed decimal (due to the cast) and as character.

void setup()
{
  byte b = 0x30;
  char buffer[32];

  Serial.begin(9600);
  sprintf(buffer, "0x%02X, %d (%d), %c", b, b, (char)b, b);
  Serial.println(buffer);
}

And to cast to a different variable

void setup()
{
  byte b = 0x81;    // b = 129
  char c;

  c = (char)b;      // c = -127

  Serial.begin(9600);
  Serial.println(b);
  Serial.println(c);
}

The data in both b and c is the same. The cast is probably not necessary in the second example.

Note: code not tested.

I am using an Xbee to transmit data and I can only pass 1 byte of data.

This is nonsense. The getData() function that you refer to returns an array of bytes. The array can contain more than one byte.

Do NOT use Strings. There is no reason to piss away resources like that.

The second code worked but needed to be tweaked slightly from "char" to "signed char". Thanks!!

@ PaulS, String is easier to manipulate than Array's. I am only storing 1 string and using that 1 String, deleting, adding to, concatinating, searching through, parsing, and sending that string over serial to another MCU. Teensy to Esp8266 chip. I have plenty of resources for this 1 String.

sterretje:
You can cast; that tells the compiler to treat the byte as a character.

Below code prints the content of byte b in hex, unsigned decimal (as it's a byte), signed decimal (due to the cast) and as character.

void setup()

{
  byte b = 0x30;
  char buffer[32];

Serial.begin(9600);
  sprintf(buffer, "0x%02X, %d (%d), %c", b, b, (char)b, b);
  Serial.println(buffer);
}




And to cast to a different variable


void setup()
{
  byte b = 0x81;    // b = 129
  char c;

c = (char)b;      // c = -127

Serial.begin(9600);
  Serial.println(b);
  Serial.println(c);
}



The data in both b and c is the same. The cast is probably not necessary in the second example.

Note: code not tested.

brolly759:
@ PaulS, String is easier to manipulate than Array's. I am only storing 1 string and using that 1 String, deleting, adding to, concatinating, searching through, parsing, and sending that string over serial to another MCU. Teensy to Esp8266 chip. I have plenty of resources for this 1 String.

And every time you delete, add to, or concatenate with that String it has to be reallocated on the heap causing memory fragmentation until eventually your one string is possibly wasting all the memory there.