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.

ascii conversion code samples

Status
Not open for further replies.

userx51

Junior Member level 2
Joined
Apr 21, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,432
hi,
I am very new to C and inside MCU, I need to convert from bin2ascii and ascii2bin in order to read and write to hyperterminal?. any sample codes in C?.:?:
 

yura717

Advanced Member level 2
Joined
Aug 24, 2009
Messages
690
Helped
90
Reputation
178
Reaction score
89
Trophy points
1,308
Location
USA
Activity points
4,587
Last edited:

fouwad

Full Member level 4
Joined
Nov 29, 2009
Messages
199
Helped
19
Reputation
38
Reaction score
17
Trophy points
1,298
Location
Pakistan
Activity points
2,466
2^0*i[0]+ 2^1*i[1]+ 2^2*i[2]+ and so on........
 

userx51

Junior Member level 2
Joined
Apr 21, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,432
I am sorry, how do i convert 2F to ascii. and how to convert it back to bin in C language. sorry for my lack of familiarity. just started reading about it. within the MCU. I work with hyper terminal. experimenting motor motion.
 

embpic

Advanced Member level 3
Joined
May 29, 2013
Messages
742
Helped
80
Reputation
160
Reaction score
77
Trophy points
1,308
Location
india
Activity points
5,213
as give example if u want to send 2F then senf lsb nibble by using masking of 0x0f and again shift msb nibble to lsb side again do same.

example:

unsigned char ascii_array[16]={"0123456789ABCDEF"};
void send_ascii(unsigned char dat)
{
unsigned char temp;
temp = dat & 0x0F;
tx(ascii_array[temp]);

temp = dat >> 4;
temp = temp & 0x0f;
tx(ascii_array[temp]);
}

- - - Updated - - -

and if u want to convert it to in bin form then

if(num <9 && num >0)
num = num-'0';

if num is A to F then minus 'A'

and then again combine it in reverse manner
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,153
Helped
4,944
Reputation
9,909
Reaction score
4,755
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
128,697
If you just want single character/digit conversions, these should work:

Code:
//*******************************************************************************
//
char AscToBin(char AsciiChar)
{
	if(AsciiChar > '9') AsciiChar -= 7;
	return((AsciiChar - '0') & 0x0F);
}

//*******************************************************************************
//
char BinToAsc(char BinValue)
{
	BinValue &= 0x0F;
	if(BinValue > 9) BinValue += 7;
	return(BinValue + '0');
}

Call the appropriate one with the character/digit as the parameter and it will return the converted version, for example:

AsciiCharacter = BinToAsc(5); Will return the Ascii character '5' (0x35)

BinaryValue = AscToBin('A'); will return 0x0A

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top