CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Hex to ASCII

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

Hex to ASCII
PostPosted: Sun Nov 04, 2018 1:34 pm     Reply with quote

Hi, I have this problem

I read a module RFID

//!Numero tarteja: 0009531147
//!Dato : 00 91 6f 0b f5
//!check bit, f5 = 00^91^6f^0b

I capture the data:
Code:
c=getc(RFID);                         
UID[i++]=c;

So I get data 00 91 6f 0b f5

now for print I use this
Code:
for (k=0;k<5;k++) fprintf(debug,"%c",UID[k]);   //  No print characters, none print

When I use:

Code:
for (k=0;k<5;k++) fprintf(debug,"%X",UID[k]);      // IUD      print 00916f0bf5

My question is how I do for print every digit as ascii? for can use this function:
Code:

// ******* ASCCI to KEYBOARD
uint16_t ToHidKeyboardScancode(char c){
      switch(c){
      // Hex
         case 'A':   return(0x04);
         case 'B':   return(0x05);
         case 'C':   return(0x06);
         case 'D':   return(0x07);
         case 'E':   return(0x08);
         case 'F':   return(0x09);

         case '1':   return(0x1E);
         case '2':   return(0x1F);
         case '3':   return(0x20);
         case '4':   return(0x21);
         case '5':   return(0x22);  //
         case '6':   return(0x23);  //
         case '7':   return(0x24);  //
         case '8':   return(0x25);  //
         case '9':   return(0x26);  //
         case '0':   return(0x27);
      }
      //if ((c>='a') && (c<='z')) return (c-'a'+'A');
      return(0);

}

Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Nov 04, 2018 2:12 pm     Reply with quote

No. That's code to convert to a keyboard scancode.

Your first print line should print something. It won't work 'right', since code 00 is normally an unprintable (NUL) code, while 09, is a tab character, 53 is a capital S, then 11 is a 'DC1' control code whose function will depend on the terminal program being used, then the final character is a G.
So what you will get will depend strongly on the program being used. but is unlikely to be anything useful.
Since the codes are not all 'displayable' ASCII characters, there is not a real answer to your question. You'd have to work out what you want to show for each value, and what code your target program needs to display this, and translate each byte to a printable value.
Three of your characters are 'non printable' characters.
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sun Nov 04, 2018 2:19 pm     Reply with quote

Ttelmah wrote:
No. That's code to convert to a keyboard scancode.

Your first print line should print something. It won't work 'right', since code 00 is normally an unprintable (NUL) code, while 09, is a tab character, 53 is a capital S, then 11 is a 'DC1' control code whose function will depend on the terminal program being used, then the final character is a G.
So what you will get will depend strongly on the program being used. but is unlikely to be anything useful.
Since the codes are not all 'displayable' ASCII characters, there is not a real answer to your question. You'd have to work out what you want to show for each value, and what code your target program needs to display this, and translate each byte to a printable value.


Thanks

I need print every character as press keyboard...
0 as 0, 9 as 9 depend of IUD get... for example
IUD= 00916f0bf5

I need print 0, 0, 9,1.... with the scancode....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 04, 2018 4:57 pm     Reply with quote

The program below displays:
Quote:
00916F0BF5

Test program:
Code:
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)

#define TO_HEX(i) ((i) <= 9 ? ('0' + (i)) : ('A' - 10 + (i)))

//=======================================
void main()
{
int8 k;
int8 UID[5]= {0x00, 0x91, 0x6f, 0x0b, 0xf5};

for(k=0;k<5;k++)
   {
    putc(TO_HEX(UID[k] >> 4));   
    putc(TO_HEX(UID[k] & 0x0F));   
   }
 
while(TRUE);
}
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sun Nov 04, 2018 6:33 pm     Reply with quote

PCM programmer wrote:
The program below displays:
Quote:
00916F0BF5

Test program:
Code:
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)

#define TO_HEX(i) ((i) <= 9 ? ('0' + (i)) : ('A' - 10 + (i)))

//=======================================
void main()
{
int8 k;
int8 UID[5]= {0x00, 0x91, 0x6f, 0x0b, 0xf5};

for(k=0;k<5;k++)
   {
    putc(TO_HEX(UID[k] >> 4));   
    putc(TO_HEX(UID[k] & 0x0F));   
   }
 
while(TRUE);
}


Thanks you so much! this help me. Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 3:46 am     Reply with quote

In fact your original approach would do this:
Code:

for(k=0;k<5;k++)
   {
    printf("%02X",UID[k]);
   }


Does exactly the same....

and (of course), if you wanted each byte displayed as it is input, you can put this in the reading code:
Code:

c=getc(RFID);                         
UID[i++]=c;
printf("%02X",c);


However 'beware'. Remember this generates two output bytes for each arriving byte, so if the data arrived really quickly, there might not be time to send.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 9:28 am     Reply with quote

Right, but I assumed that he wanted individual nybbles. That's the way
he talked when he posted:
Quote:
I need print 0, 0, 9,1.... with the scancode....

My code gives access to individual nybbles.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 11:25 am     Reply with quote

But you can't have single nibbles. It all only works once a whole byte is received!.... Smile
Hence your twin putc.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 11:42 am     Reply with quote

But with my code, you can display each nybble, if you desire it.
His example shows commas between each nybble. My code can
be modified to do that.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 12:13 pm     Reply with quote

Okay. Makes sense. Seems pointless since the data is coming in bytes, but I can see why you gave that solution.
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 11:47 pm     Reply with quote

Ttelmah wrote:
Okay. Makes sense. Seems pointless since the data is coming in bytes, but I can see why you gave that solution.



Thanks to everyone ... I have the solution Smile

Just it this:
Code:


#define TO_HEX(i) ((i) <= 9 ? ('0' + (i)) : ('A' - 10 + (i)))

ToHidKeyboardScancode(TO_HEX(character >> 4))
ToHidKeyboardScancode(TO_HEX(character & 0x0F));
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Nov 05, 2018 11:56 pm     Reply with quote

OK. This can equally well be done with:
Code:

printf(ToHidKeyboardScancode,"%02X",character);


What you have omitted to tell us, is that you want the data sent to a PC
as a USB keyboard input. You talk about ASCII, but this isn't.
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Tue Nov 06, 2018 12:20 am     Reply with quote

Ttelmah wrote:
OK. This can equally well be done with:
Code:

printf(ToHidKeyboardScancode,"%02X",character);


What you have omitted to tell us, is that you want the data sent to a PC
as a USB keyboard input. You talk about ASCII, but this isn't.


Perfect Smile

I said accii because the function say "asccii to code" Rolling Eyes
Sorry I am learning Smile but thanks you so much. Now I can type every byte that coming from module RFID without problem
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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