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.

[SOLVED] problem in my hex to ascii converter C code

Status
Not open for further replies.

Embedded Partner

Full Member level 5
Joined
Nov 29, 2010
Messages
247
Helped
42
Reputation
84
Reaction score
37
Trophy points
1,308
Location
Hubli(INDIA)
Activity points
2,636
i m facing problem in C code
if i give input as some data i m getting different values on hyperterminal
input output
0x1234 1334
ox9999 9999
0x2389 23;9
0x1111 1111
0x2222 2222
9876 99?6
6789 67?9
so on

my code is



void main()
{
unsigned long int value;
value=hex_to_ascii(0x1234);
display_hyperterminal(value);
}

unsigned long int hex_to_ascii(unsigned int HEX)
{
unsigned long int FinalData;
unsigned int mov;
unsigned char Data1,Data2,Data3,Data4; // dummy variable to hold 16 bit hex no


mov=HEX & 0xF000;
mov = mov>>12;
Data1=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x0F00;
mov =mov>>8;
Data2=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x00F0; // masking the higher LSB
mov=mov>>4; // shifting to the lower LSB
Data3=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x000F;
Data4=(mov>0x0009)?(mov+0x37):(mov+0x30);

FinalData= Data1;
//lcd_printc(Data1);
FinalData=FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data2;
//lcd_printc(Data2);
FinalData=FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data3;
//lcd_printc(Data3);
FinalData=FinalData | FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data4;
//lcd_printc(Data4);
return FinalData;
}
 
Last edited:

i m facing problem in C code
if i give input as some data i m getting different values on hyperterminal
input output
0x1234 1334
ox9999 9999
0x2389 23;9
0x1111 1111
0x2222 2222
9876 99?6
6789 67?9
so on

my code is



void main()
{
unsigned long int value;
value=hex_to_ascii(0x1234);
display_hyperterminal(value);
}

unsigned long int hex_to_ascii(unsigned int HEX)
{
unsigned long int FinalData;
unsigned int mov;
unsigned char Data1,Data2,Data3,Data4; // dummy variable to hold 16 bit hex no


mov=HEX & 0xF000;
mov = mov>>12;
Data1=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x0F00;
mov =mov>>8;
Data2=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x00F0; // masking the higher LSB
mov=mov>>4; // shifting to the lower LSB
Data3=(mov>0x0009)?(mov+0x37):(mov+0x30);

mov=HEX & 0x000F;
Data4=(mov>0x0009)?(mov+0x37):(mov+0x30);

FinalData= Data1;
//lcd_printc(Data1);
FinalData=FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data2;
//lcd_printc(Data2);
FinalData=FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data3;
//lcd_printc(Data3);
FinalData=FinalData | FinalData<<8;
FinalData=FinalData & 0xffffff00;
FinalData=FinalData | Data4;
//lcd_printc(Data4);
return FinalData;
}

What exactly you want to achieve? Do you want 0x1234 should be displayed as 1234?
if so then try to return a string like sprintf()
and remove the "0x" from the begining.

That is the easiest way I can perceive.

next assuming you "do not want" to use sprintf() then you can do a lookup table kind of thing and print the coresponding character.

I guess there is no problem in your code. The Hyper terminal setting is most probably changing the characters to some thing else.

Hope this helps.
 
thank you so much

can you write program for displaying long int on the hyperterminal
coz i m trying to display this long int
 

my hyper terminal is working fine

R=hex_to_ascii(buffer2);
a1=0x000000FF & R;
R=R>>8;
a2=0x000000ff & R;
R=R>>8;
a3=0x000000ff & R;
R=R>>8;
a4=0x000000ff & R;
TranferByteSerial('*');
TranferByteSerial(a4);
TranferByteSerial(a3);
TranferByteSerial(a2);
TranferByteSerial(a1);

---------- Post added at 08:21 ---------- Previous post was at 07:24 ----------

my code is working 5n now
 

my hyper terminal is working fine

R=hex_to_ascii(buffer2);
a1=0x000000FF & R;
R=R>>8;
a2=0x000000ff & R;
R=R>>8;
a3=0x000000ff & R;
R=R>>8;
a4=0x000000ff & R;
TranferByteSerial('*');
TranferByteSerial(a4);
TranferByteSerial(a3);
TranferByteSerial(a2);
TranferByteSerial(a1);

---------- Post added at 08:21 ---------- Previous post was at 07:24 ----------

my code is working 5n now


Hi Embedded partner;
Nice to know that your code is working :-D

Well May I ask for what purpose you wrote the code?
Is it for debug purpose to see how far you proceeded in you code?

If show you can just send a character to the Hyper terminal.
Of course if you want to know the value of certain variable, then you might send the HEX.

Thanks
ashok.

---------- Post added at 08:57 ---------- Previous post was at 08:50 ----------

hi ajit back,
You can see my post above your post, and you can convert the long int to a string and display it on the hyper term.
just you have to do a sprintf().

I don't think it is very hard to write a
Code:
 sprintf ( buffer,"%d",my_longint);[\code]
and send it to your comport.

Hope this helps?
 

The ASCII code for a '0' character has a value of 0x30
The ASCII code for a '1' character has a value of 0x31
etc
so to convert a hex number (0-9) to ASCII you have to add 0x30
also
The ASCII code for a 'A' character has a value of 0x41;
The ASCII code for a 'B' character has a value of 0x42;
etc
so to convert a hex letter (A-F) to ASCII you have to add 0x37

to show a hex value you can send the byte to the function and in the function
you have to break the 0xff to 0x0f and 0x0f
Code:
nibble_low=(received_hex & 0x0f);
nibble_high=(received_hex >>4);

if (nibble_low <= 9) {
	nibble_low+=0x30;}
else {
	nibble_low+=0x37;}

if (nibble_high <= 9) {
	nibble_high+=0x30;}
else {
	nibble_high+=0x37;}

so now nibble_low is the lower nibble (4bits) converted to ASCII
and nibble_high is the high nibble converted to ASCII.

There are different ways to do the function, you can make a function that gets a nibbles and returns a character, or gets the byte and returns 2 bytes or maybe can get a 4 byte input and return a 8 byte ASCII result.

Alex
 

Code:
#include<stdio.h>
#include<string.h>
char buffer[9];
void hex2ascii(unsigned long my_int)
{
	/* in linux long is 4 bytes*/
	unsigned char nibble=0;
	int index;
	for( index=0; index <8; index++)
	{
		nibble=my_int&0x0000000f;
		printf("nib=%d\n",nibble);
		if(nibble<9)
			nibble+='0'; // add char zero ascii value
		else
			nibble+=55;// add char "A"
		buffer[8-index-1]=nibble;
		my_int=my_int>>4;	// shift left 4 places
		printf("%c, %d myint=%x\n ",nibble,nibble,my_int);
	}
}
void hex2string(unsigned long my_int)
{
	char temp[9];
	sprintf(temp,"%x",my_int);
	strncpy(buffer,temp,8);
}

int main()
{
	unsigned long n=0x12bfefed;
	hex2ascii(n);
	printf("%s\n",buffer);
	n=0xfe98beaf;
	hex2string(n);
	printf("%s\n",buffer);
}

Guess this will be helpfull.

Ashok.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top