Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining two values

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi Salem and all,

How can I cat two bytes which is read in as char and then converted in hex, for e.g.

If I read the first byte (char) and then covert it to hex (26), read
the next char and convert it to hex (ac). Cat the both should be 26ac , then only
print the decimal value.

Is my logic correct or is there a different way of doing this


Please help
Chris

Code:
if(j > 67 && j < 70)
	{
		if(j==68)
		{
			printf(" ");
		}

		sprintf(datum, "%x" , xya[j]);
		xy1=fgetc(in);
		sprintf(datum_1, "%x" , xy1);
		
		// this is where i get stuck
		cat datum datum_1
		sprintf (SCV ,"%d" , result fom cat ); 
		//
		
		printf("%d", datum);
	}
 
That would be
Code:
int value = xya[j] * 256 + xy1;
sprintf (SCV ,"%d" , value );



--
 
Hi Salem,

Thanks a lot. Your first byte is xya[j] (char) which you * 256 ..ok and when I get the next byt == fgetc xy1, it is also a char??

Chris

Code:
for (j=0; j<record_len; j++)
{
	if(j < 1 )
	{
		printf(" ");
		printf("%c",xya[j]);
	}
	
...........
	if(j > 67 && j < 70)
	{
		if(j==68)
		{
			printf(" ");
		}

		
		xy1=fgetc(in);
		int value = xya[j] * 256 + xy1;
		sprintf (SCV ,"%d" , value );

		printf("%d", SCV);
	}

 
Hi Salem,


The result I get with the above code is "17615506841761550684" which is incorrect. What am I doing wrong

Thanks Again
Chris
 
> sprintf (SCV ,"%d" , value );
> printf("%d", SCV);
You're printing a string (SCV) as a decimal (%d)
Use %s in the printf()


--
 
Hi Salem,

I might be missing your point. When we first read the byte it is in char format "&" and when converted it is 26 and so this applies for the next byte "ac", so I cannot see how you would pass it as an int. Should I not convert it to hex or straight into int then only times it by 256. If soo, how can I do this.

Thanks
Chris
 
Try something like this:
Code:
int num = 0;
...
strcat( datum, datum_1 );
sscanf( datum, "%x", num );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top