Pages: [1]   Go Down
Author Topic: byte contains negative DEC values  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hmmmm
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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:
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);
       Serial.print(",");
     }
 

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

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
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Quote
Serial.print(packetdata,DEC);
print prints characters.
write will write binary data.

EDIT: I think the above two statements only apply to IDE version 1.0 and up
« Last Edit: May 24, 2012, 09:18:25 pm by pYro_65 » Logged


Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hmmmm
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks pYro_65

I think I have resolved the issue now

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

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

Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2109
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: