Re: How to convert numbers from Binary to Decimal? Please Coment!:D

Member.

In another post called radio.write I was told that the data I received is in binary. I coppied the above code mentioned in #3 into my receiver and it only returns a numbr of 170. It should print a decimal vaue of -0.61

The data I receive currently is gubberish.

Any ideas on how to fix this?

Thanks in advance

fnb111:
Member.

In another post called radio.write I was told that the data I received is in binary. I coppied the above code mentioned in #3 into my receiver and it only returns a numbr of 170. It should print a decimal vaue of -0.61

The data I receive currently is gubberish.

Any ideas on how to fix this?

Thanks in advance

ALL integer values are stored as binary - it is the ONLY way they can stored in a digital MCU.

The difference arises when you print them out to some sort of display device.

In the case of serial monitor you can chose to display the value as the native binary number: Serial.println(10, BIN)

Or as a more intuitive decimal number Serial.println(10, DEC)....or just Serial.println(10)

When reading integer data from a hardware device you simply don't need to concern yourself as to whether the data is binary or decimal. Such a concern is totally irrelevant.

Just declare an integer variable or variables and read the integer values into those variables.

Then you can chose how you want to display those rad values in Serial monitor for example - as a binary number of a decimal number.

decimal = (bA[7] * 128) + (bA[6] * 64) + (bA[5] * 32) + (bA[4] * 16) +(bA[3] * 8)+ (bA[2]*4) +(bA[1]*2)+ bA[0];

Given the array: booleanArray = {1,0,1,0,1,0,1,0};
The OP wants the corresponding decimal number which is , by manual calculation, 170.

The quoted expression, upon manual computation, gives:
1128 + 064 + 132 + 016 + 18 + 04 + 12 + 00 = 128 + 32 + 8 + 2 = 170

The quoted expression, upon computer based computation, gives:
byte x = 0
x = x + 128; //x = 0x80 ; because the CPU will do binary addition
x = x+ 32; //x= 0x80 + 32 = 0xA0
x = x + 8; //x = 0xA0 = 8 = 0xA8
x = x+2; //x = 0xA8 + 2 = 0xAA

I am back to 10101010 (0xAA); where is the expected 170! :slight_smile:

So, trick to obtain decimal (aka BCD) value by adding the respective positional values (positional factor*positional weight) will fail unless provision is kept for the necessary adjustment while the partial decimal (aka BCD) result is about to switch from 9 to 10 (9 + 1 ----> A). The 8085 has DAA, the 80x86 has DAA, the 8051 has DA A, and the ATmega has none for this adjustment.

Thanks for the explanation. I tried the code to serial.print(10,DEC) and no change. This is a copy of the Tx

//transmitter_ASC basic

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(6,7);

const byte rxAddr[6] = "00001";
const int analogIn = A0; //ASC758 connection
int mVperAmp = 20;  // scale factor
int ACSoffset = 2500; //see bi directional offset
int RawValue = 0;
double Voltage = 0;
double Amps = 0;

void setup()
{
  Serial.begin(9600);
radio.begin();
radio.setRetries(15, 5);//time,re tries
radio.openWritingPipe(rxAddr);

radio.stopListening();
}

void loop() 
{

 RawValue = analogRead(analogIn);
 Voltage =(RawValue/1023.0)*5000; //gets mV
 Amps =((Voltage-ACSoffset)/mVperAmp);


const char text[] = "Amps1 ="; // writes Amps1= to Rx unit
radio.write(&text, sizeof(text)); //check ())- its correct
//delay(50);

radio.write (&Amps, (sizeof Amps));

delay(3000);

}

As explained on your other thread on this topic, you need to post both the transmitter and receiver code.
Note that you are.not just dealing with a binary number, you are dealing with a floating point binary number.
This shouldn't be a problem, because computers are really good at doing simple things like format conversion.

#4 and #8 is the full code

As explained to you yesterday, you're sending a float, so you should receive a float.

float Amps;
radio.read(&text, sizeof(text)); //prints the word Amps1=
radio.read(&Amps, sizeof(Amps));

Serial.print(text);
Serial.println(Amps);

Note: I spotted a mistake and edited code.

I'm not sure about your handling of text.
What happens if you send less (fewer?) than 32 bytes of text?

Given the array: booleanArray = {1,0,1,0,1,0,1,0};
The OP wants the corresponding decimal number which is , by manual calculation, 170.

Note the origional posting dates. This thread was hijacked after reply #6

Grumpy_Mike:
Note the origional posting dates. This thread was hijacked after reply #6

Yeah, I hate necro-hijacks

Members
Ive tried a couple of versions like:
float Amps[32] or float Amps[32]=0 or float Amps. Ive even tried double Amp and int Amp.

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:73:12: note: size_t Print::println(char)

size_t println(char);

^

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:73:12: note: no known conversion for argument 1 from ‘float (*)[32]’ to ‘char’

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:74:12: note: size_t Print::println(unsigned char, int)

size_t println(unsigned char, int = DEC);

^

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:74:12: note: no known conversion for argument 1 from ‘float (*)[32]’ to ‘unsigned char’

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:75:12: note: size_t Print::println(int, int)

size_t println(int, int = DEC);

^

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:75:12: note: no known conversion for argument 1 from ‘float (*)[32]’ to ‘int’

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:76:12: note: size_t Print::println(unsigned int, int)

size_t println(unsigned int, int = DEC);

^

C:\Users\Johan Prinsloo\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Print.h:76:12: note: no known conversion for argument 1 from ‘float (*)[32]’ to ‘unsigned int’

I edited my last post - please re-read it

#11
I know about the &. Ive tried variouse options and the result stay the same. If I use anything other than float -ie double,then I have to add & -otherwise the program does not compile.

float does not change the result. Ive tried variouse versions and placement options. If the gubberish is in binary how do I change it to a dec?

Ive tried some code -think its #3- not sure. It returned a different value.

thanks

You should know the drill by now.
Post BOTH sketches, post observations and any screen output.

I suggest for now, you forget about sending and receiving text. Just concentrate on the floats.

#12 #16

I did change the data length. Please see attached. It only reads more [32] or less [8] gubberish

#9 #18

The sketch is in #4 & #8
Sending Text was never an issue. Thats only for my reference to verify data send.

Its day 3( aprox 44 hours on this issue) and I must say Ive never had an programming issue this hard to solve. Im beginning to think that this is one of those problems that nobody can resolve.

Doc1.pdf (218 KB)

. Im beginning to think that this is one of those problems that nobody can resolve.

You’re being ridiculous.

If you can’t be bothered to post your code, and your observations, I can’t be bothered to attempt to develop psychic powers.

//receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(6,7); //CE,CSN pins RF24L01

const byte rxAddr[6] = "00001"; //address of unit 1

 
void setup()
{
while (!Serial);
Serial.begin(9600);

radio.begin();
radio.openReadingPipe(0, rxAddr);
radio.startListening();
}

void loop()
{
if (radio.available())
{
char text[32] = {0}; // for word Amps1=
char Amps[8];
radio.read(&text, sizeof(text)); //prints the word Amps1=
radio.read(Amps, sizeof(Amps));// data received is giberish. need to clean it up

Serial.print(text);
Serial.println(Amps);
}
}

How are your comprehension skills?

You are sending a float, therefore you should be receiving a float.

I also wrote that you should be sending and receiving only floats, not text (for now)

The way you answer questions say a lot about your character

...and the way you ignore advice says even more.

@fnb111, do not cross-post.

@fnb111, do not hijack.

Thread locked.