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.

Question about ASCII conversion.

Status
Not open for further replies.

maniac84

Full Member level 6
Joined
Mar 4, 2012
Messages
337
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
3,661
Hi there. Is there a way in C coding to convert like the way like this? For example I got '1' equals to 0x31, and '2' equals to 0x32. I want to convert 0x31, 0x32 to 0x12.
 

not sure I follow the sequence; 0x31, 0x32 ...to 0x12. It increases and then decreases? can you explain the last term?

if there is no sequence or logic then use a switch statement
 

hello,


Code:
unsigned char c1,c2;
unsigned char R;
c1='1' -'0';
c2='2'-'0';
R=c1<<4 + c2;    // 0x12

or Maybe you want to get "12" ?
Code:
char Texte[16];// table of char
char *txt;  // pointer
.......
txt=&Texte[0]; // initialise pointer
*txt=c1;
*txt+1=c2;
*txt+2=0; // end of string
// now txt point on the result '12'
 

Do you want to convert the Ascii numbers to BCD arithmetic or hexadecimal?
'12' in ascii is not equeal to 0x12 in hex, the correct value here is 0x0C.

The value 0x12 is in BCD, where each nibble(4bits) in the byte is one digit from 0-9.
BCD=Binary Coded Decimal.
 

hello,


Code:
unsigned char c1,c2;
unsigned char R;
c1='1' -'0';
c2='2'-'0';
R=c1<<4 + c2;    // 0x12

or Maybe you want to get "12" ?
Code:
char Texte[16];// table of char
char *txt;  // pointer
.......
txt=&Texte[0]; // initialise pointer
*txt=c1;
*txt+1=c2;
*txt+2=0; // end of string
// now txt point on the result '12'

Your first coding is the answer I want. Thanks!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top