Go Down

Topic: Convert Hex/Binary/DEC to signed char (Read 183 times) previous topic - next topic

brolly759

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.

ieee488

#1
Apr 25, 2016, 02:55 am Last Edit: Apr 25, 2016, 02:55 am by ieee488
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




Whandall

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.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

brolly759

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

Code: [Select]
  Serial.print("payload [");
          Serial.print(i, DEC);
          Serial.print("] is ");
          Serial.println(rx.getData()[i]);
          String a = String(rx.getData()[i]);
          storedData.concat("." + a );

Whandall

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?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

brolly759

#5
Apr 25, 2016, 08:25 am Last Edit: Apr 25, 2016, 08:27 am by brolly759
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


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.

Code: [Select]

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
Code: [Select]

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

PaulS

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

brolly759

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.


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.

Code: [Select]

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
Code: [Select]

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.

Delta_G

@ 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.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy