Converting long int to hex with printf in TC 3.0

P

pranab.salian

I need to compile some newer code in Borland TC 3.0. Here's the
snippet..

/* CODE */
/*
// ---------------------------------------------------------------
// Shift register implementation: Message is divided by G. In the
// reminder, stored in the syndrome register S, are the redundant
// bits.
// ---------------------------------------------------------------
*/

S = 0;
j = K2;
for (i=15; i>=0; i--) {
bit = ( (j & M) >> i ) & 0x01;
j = j>>1;
if (bit)
S = ((S<<1)^FLAG) & NK;
else
S = (S<<1) & NK;
if (S & FLAG)
S = (S^G) & NK;
}

/* At this point a watch on "S" says S:119, and S,x:0x77 */

/* Write the generator matrix of the code: */
printf("%4x %3lx", M, S);

/* END CODE */

I had to change most int variables to long int for the code to even run
properly when compiled with TC 3.0. Now the problem is when the printf
converts the long S to hex, instead of the expected "77" it prints
"770000". I need a way to stop it from printing the trailing zeroes.

Oh BTW it is "lx" in "%3lx" because for some reason I can't figure, if
I use "%3x" it just prints a "0". I would expect it to print "77".
 
R

Robert Gamble

I need to compile some newer code in Borland TC 3.0. Here's the
snippet..

/* CODE */
/*
// ---------------------------------------------------------------
// Shift register implementation: Message is divided by G. In the
// reminder, stored in the syndrome register S, are the redundant
// bits.
// ---------------------------------------------------------------
*/

S = 0;
j = K2;
for (i=15; i>=0; i--) {
bit = ( (j & M) >> i ) & 0x01;
j = j>>1;
if (bit)
S = ((S<<1)^FLAG) & NK;
else
S = (S<<1) & NK;
if (S & FLAG)
S = (S^G) & NK;
}

/* At this point a watch on "S" says S:119, and S,x:0x77 */

/* Write the generator matrix of the code: */
printf("%4x %3lx", M, S);

/* END CODE */

I had to change most int variables to long int for the code to even run
properly when compiled with TC 3.0. Now the problem is when the printf
converts the long S to hex, instead of the expected "77" it prints
"770000". I need a way to stop it from printing the trailing zeroes.

Oh BTW it is "lx" in "%3lx" because for some reason I can't figure, if
I use "%3x" it just prints a "0". I would expect it to print "77".

The %x conversion specifier expects an unsigned int argument, %lx
expects an argument of type unsigned long. Although you should be okay
passing a long signed int for a %lx conversion, as long as value is
non-negative, passing a long value for the %x conversion invokes
undefined behavior.

In any case, I don't know why you would expect %3lx to ever print "77",
in your example you should expect it to print " 77" as you specify 3 as
the minimum field width.

My guess (and it is a guess because you did not provide a complete
compilable example that provides the definitions of M and S) is that
both M and S are long ints, sizeof(long) > sizeof(int) on your system,
and the %4x conversion doesn't read the entire corresponding value.
The %3lx conversion reads the leftover bytes from M along with some of
the bytes from S resulting in what you are seeing. Changing your print
statement to printf("%4lx %3lx", M, S) should solve your problem.

The following program should result in " 77" being printed:

#include <stdio.h>

int main (void) {
printf("%3lx\n", 119LU);
return 0;
}

If it doesn't then your implementation is broken.

Robert Gamble
 
R

Robert Harris

I need to compile some newer code in Borland TC 3.0. Here's the
snippet..

/* CODE */
/*
// ---------------------------------------------------------------
// Shift register implementation: Message is divided by G. In the
// reminder, stored in the syndrome register S, are the redundant
// bits.
// ---------------------------------------------------------------
*/

S = 0;
j = K2;
for (i=15; i>=0; i--) {
bit = ( (j & M) >> i ) & 0x01;
j = j>>1;
if (bit)
S = ((S<<1)^FLAG) & NK;
else
S = (S<<1) & NK;
if (S & FLAG)
S = (S^G) & NK;
}

/* At this point a watch on "S" says S:119, and S,x:0x77 */

/* Write the generator matrix of the code: */
printf("%4x %3lx", M, S);

/* END CODE */

I had to change most int variables to long int for the code to even run
properly when compiled with TC 3.0. Now the problem is when the printf
converts the long S to hex, instead of the expected "77" it prints
"770000". I need a way to stop it from printing the trailing zeroes.

Oh BTW it is "lx" in "%3lx" because for some reason I can't figure, if
I use "%3x" it just prints a "0". I would expect it to print "77".
Your problem is probably that M is defined as a long int but your printf
only consumes an int's worth of bits for it.

You need to include all your declarations for anybody to help further.

Robert
 
P

pranab.salian

Robert said:
My guess (and it is a guess because you did not provide a complete
compilable example that provides the definitions of M and S) is that
both M and S are long ints, sizeof(long) > sizeof(int) on your system,
and the %4x conversion doesn't read the entire corresponding value.
The %3lx conversion reads the leftover bytes from M along with some of
the bytes from S resulting in what you are seeing. Changing your print
statement to printf("%4lx %3lx", M, S) should solve your problem.


Thanks a *ton*, Robert.. Since the value of M was printing correctly, I
didn't realise that it could be an error in M that would be affecting
the way the value of S was printed. Which is why I didn't bother
including the type of M..

I've made the necesssary corrections and it's working perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top