473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7073
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5407
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
29
19556
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
3
6877
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
4
8636
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
18
26659
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
11
3341
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
116
35678
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
1
5640
by: jamesd | last post by:
First off my programming experience is very limited and I haven't used C/C++ in the past 4/5 years so I'm fairly c**p at it. Basically I'm trying to write a function that opens a .wav file and...
9
5151
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.