Go Down

Topic: byte contains negative DEC values (Read 915 times) previous topic - next topic



I am having a problem of when I do a serial.print(data[4],DEC) from a char/byte array I get strange values. So sometimes instead of getting a "255" I get a "-1"

I have tried using unsigned int instead of int but instead of getting "-1" I get numbers like 65525.

replacing serial.print(packetdata[ i],DEC) with serial.print(packetdata[ i],HEX) also give bad values like "FFFFFFDA" instead of something like "DA"

Code: [Select]

void Sendpacket(unsigned int toaddr,byte data[11] )   
  byte packetdata[16];
  packetdata[0] = toaddr ;     
   for (i = 0; i < 11; i = i + 1) {
      packetdata[5+i] = data[i];
     for (i = 5; i < Mirf.payload; i = i + 1) {
       unsigned int val = packetdata[i];
       Serial.print(val);                                                    // this is to trye and fix the issue of 255 sometimes being displayed as -1, but now gets displayed as like 65501
//      Serial.print(packetdata[i],DEC);

void loop(){
    byte message[11] = "          ";     
    int BatteryVoltage = readVcc();
    message[7] = (BatteryVoltage >> 8) & 0xFF, BYTE;   
    message[8] = BatteryVoltage & 0xFF, BYTE;

I am still using version 22 of Arduino due to some of the libraries I require not supporting version 1.0
Below is some of the code I use, I am using it with MIRF but the data is going screwy before it even gets to MIRF


May 25, 2012, 04:13 am Last Edit: May 25, 2012, 04:18 am by pYro_65 Reason: 1
from a char/byte array

char and byte are not the same,
char is default signed and byte is defined as 'unsigned char'.char will give negative numbers, byte won't.

unsigned | signed
255 == 0xFF == -1


print prints characters.
write will write binary data.

EDIT: I think the above two statements only apply to IDE version 1.0 and up
Forum Mod anyone?


Thanks pYro_65

I think I have resolved the issue now

now instead of
byte packetdata[16];
char packetdata[16];

I am now using as below and it seems to be working now
unsiged char packetdata[16];


Hope you solved the problem.

I would encourage you to use version 1.0, it is quite easy to upgrade your libraries, there are many posts about library errors and how to fix them for 1.0. If you search "<your library name> 1.0" you will probably find an answer straight away unless you are making use of rarely used libraries.

But the big point is things like serial have changed, so upgrading a large project could be a pain if it uses any modified features. The next releases should be backwards compatible with 1.0, meaning in the future you can upgrade Arduino again without having to modify your sketch code... Hopefully.
Forum Mod anyone?

Go Up