473,661 Members | 2,431 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 7094
pr***********@g mail.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***********@g mail.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
5423
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 typedef struct Rec { unsigned char msg; unsigned long len;
29
19586
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 gcc compiler that comes with RedHat linux 9. If not, what data type will yield the largest unsigned integer for me? Thanks for your help,
3
6883
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 using fprintf. The problem is that I don't know how to convert a field of 6 octets? Should I use a long variable? Thanks
4
8653
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 int array byte for example: byte=0xEFBB byte=0xBF0D
18
26704
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
3356
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. Normally I would do this using sprintf as so: sprintf(&buffer, "%lu", integer); The problem is that I will not know if the integer values I am converting are long or long long. I also do not know if the compilers being used will support long long...
116
35836
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
1
5676
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 store the binary version of this file in a variable named 'input'. 'input' is a x integer array. 'num' is the no. of bits in the array divided by 16 (rounded up). The following is my code: using namespace std; FILE *fp; int main(int argc,...
9
5158
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 level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right values,where as floating point just prints 0.000000 to the screen.Given below is a sample program...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
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.