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.
 

kam1787

Advanced Member level 3
Joined
Jul 5, 2010
Messages
928
Helped
167
Reputation
340
Reaction score
168
Trophy points
1,323
Location
English
Activity points
5,825
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
 

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,298
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'
 

Gorgon

Full Member level 6
Joined
Nov 10, 2005
Messages
345
Helped
75
Reputation
150
Reaction score
67
Trophy points
1,308
Location
Norway
Activity points
3,666
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.
 

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
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

Top