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.

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?.:?:
 

2^0*i[0]+ 2^1*i[1]+ 2^2*i[2]+ and so on........
 

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.
 

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
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top