469,613 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Converting long int to hex with printf in TC 3.0

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".

Jun 4 '06 #1
3 6747
pr***********@gmail.com wrote:
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

Jun 4 '06 #2
pr***********@gmail.com wrote:
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
Jun 4 '06 #3

Robert Gamble wrote:
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.

Jun 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jagmeena | last post: by
29 posts views Thread by Richard A. Huebner | last post: by
3 posts views Thread by Golan | last post: by
4 posts views Thread by Lingyun Yang | last post: by
18 posts views Thread by No Such Luck | last post: by
11 posts views Thread by TomServo | last post: by
116 posts views Thread by Dilip | last post: by
9 posts views Thread by ssubbarayan | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.