|
|
View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Hex to binary converter |
Posted: Mon May 15, 2017 2:29 am |
|
|
Hi,
I'm trying to convert Hexadecimal to Binary numbers.
However, when i enter hex number such as FF it will reply ERROR.
Why is this happening? It should reply 11111111
Kindly please help me.
Here is my code:
Code: |
#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#define MAX 5
void main()
{
char c;
unsigned char d;
unsigned char key;
unsigned char hexa[MAX];
long int i=0;
usb_init_cs();
while (TRUE)
{
usb_task();
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if (c=='\n') {putc('\r'); putc('\n');}
if (c=='\r') {putc('\r'); putc('\n');}
while(true)
{
ART:
d = usb_cdc_getc();
if(d=='D')
{
while (key!=32) // push SPACE BAR to stop
{
printf(usb_cdc_putc,"\n ENTER HEX VALUE : ");
get_string_usb(hexa,3);
printf(usb_cdc_putc," Binary Value : ");
while (hexa[i])
{
switch(hexa[i])
{
case '0':
printf(usb_cdc_putc,"0000");
break;
case '1':
printf(usb_cdc_putc,"0001");
break;
case '2':
printf(usb_cdc_putc,"0010");
break;
case '3':
printf(usb_cdc_putc,"0011",);
break;
case '4':
printf(usb_cdc_putc,"0100");
break;
case '5':
printf(usb_cdc_putc,"0101");
break;
case '6':
printf(usb_cdc_putc,"0110");
break;
case '7':
printf(usb_cdc_putc,"0111");
break;
case '8':
printf(usb_cdc_putc,"1000");
break;
case '9':
printf(usb_cdc_putc,"1001");
break;
case 'A':
printf(usb_cdc_putc,"1010");
break;
case 'B':
printf(usb_cdc_putc,"1011");
break;
case 'C':
printf(usb_cdc_putc,"1100");
break;
case 'D':
printf(usb_cdc_putc,"1101");
break;
case 'E':
printf(usb_cdc_putc,"1110");
break;
case 'F':
printf(usb_cdc_putc,"1111");
break;
default:
printf(usb_cdc_putc,"\n ERROR %c",hexa[i]);
}
i++;
}
} key=0; // to initialize 'key' not as SPACE BAR
}
}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9272 Location: Greensville,Ontario
|
|
Posted: Mon May 15, 2017 5:35 am |
|
|
this...
while (hexa[i])
just doesn't seem correct or proper to me.
WHILE normally needs a 'condition' (IE: X <=64 ) though it does accept 1 or true, I'm wondering how it handles 'hexa[i]'.
You don't show us but do single characters (0...F) work fine? do 3 characters say 123 decode properly?
Also, I'd put a space in the display ( 0101 1100 instead of 01011100 ), makes it easier to read and debug.
Jay |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Mon May 15, 2017 7:22 am |
|
|
One thing you can do to improve your chances of figuring this out is change your debug line:
Code: |
printf(usb_cdc_putc,"\n ERROR %c",hexa[i]);
|
to
Code: |
printf(usb_cdc_putc,"\n ERROR %02x",hexa[i]);
|
This will output the hex of the value being seen by your logic. %c won't always be useful because a lot of characters are not displayable. With the hex value, you can go check an ASCII chart and see exactly what value is sneaking into your stream. It could simply be a left over whitespace character (like new line or carriage return). |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Mon May 15, 2017 6:31 pm |
|
|
Hi,
If i put F, FF or FFF the result will display Binary : ERROR
When I change %c to %02x , result is
Code: |
Binary : ERROR e1 ERROR 04 ERROR c3 ERROR 05 ERROR f3 ERROR 20 ERROR 0e ERROR 04 ERROR 6f ERROR 02 ERROR b5 ERROR 05 ERROR d0 ERROR 02 ERROR b3 ERROR 07
|
Which part cause the problem ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 15, 2017 7:02 pm |
|
|
One problem is that you initialize 'i' to 0 at the start of your program and
then you never reset it again.
This stuff here is illusory:
It makes you think you've taken care of 'i', but in reality you have a
while() loop that parses the string. You need to reinitialize 'i' at the start
of each new string. You need to add the line shown in bold below:
Quote: |
printf(usb_cdc_putc,"\n ENTER HEX VALUE : ");
get_string_usb(hexa,3); // Get a new hex string
printf(usb_cdc_putc," Binary Value : ");
i = 0;
while (hexa[i]) // Scan the new string until a 0x00 is found
{
switch(hexa[i])
{
case '0': |
|
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Mon May 15, 2017 7:41 pm |
|
|
Hi PCM,
It works!!! Thanks PCM. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|