Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

binary char array representation to hex ? ( what's wrong with this C code )

Status
Not open for further replies.

Pigi_102

Member level 2
Member level 2
Joined
Nov 14, 2007
Messages
46
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,721
Hi all,
I have an arduino sketch code that works well and I'm convertin in c ansi but I'm having a (some) problem.

My input data is a char array like this:
Code:
pkt="10100000"

in arduino c I have this code:
Code:
int ret

 for (i = 0; i <  8; i++) {
    ret = (ret << 1) ^ ((pkt[i] & B00010000) ? 1 : 0);
 }
and the ret value, out of the loop is 160 ( which is correct ).

I have "translated" in:
Code:
 for (i = 0; i <  8; i++) {
    ret = (ret << 1) ^ ((pkt[i] & 0b00010000) ? 1 : 0);
 }

but ret value is 255, which obviously is wrong.

What I'm doing wrong ?

Thanks in advance

Pierluigi
 
Last edited by a moderator:

Your code is completely wrong.
you binary AND an ascii val (48 or 49 if I remember correctly) with one bit in pos 5 (val is 16 and that produces TRUE for both zero or one ascii vslues). Xor is also questionable in this position.

- - - Updated - - -

Have a look at my code in:
https://www.edaboard.com/threads/340997/#post1454632
 
xenos,
your code work.
Still can't understand the error in my code, but for the moment I'm fine with that.


Pihi
 

...
Code:
pkt[i] & B00010000
...

pkt has eigther 48 or 49 (ie eighter b0110000 or b0110001)
what should the (b00010000 & b0110000) produce as output?
what should the (b00010001 & b0110000) produce as output?

I do not see how this code should work in arduino.

Try replacing the b00010000 in your code with b00000001 (or just 1 ;-))
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top