473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Long Integer being cast as integer

Hi All,
I am writing a function at the moment to calculate the value of an byte offset for a floppy disk image but for some reason the long integer I am using to store this value is signed despite explicitly declaring it as unisgned. Also where the function value is return to, if this is given the correct value there is no problem so it appears to be limited to this function. Any ideas?
Code is:
Expand|Select|Wrap|Line Numbers
  1. find_offset(sect, cyl, hd)
  2. char sect, hd, cyl;
  3. {
  4.     char sectorsize = 512;
  5.     unsigned long offset = 0;
  6.     offset = (cyl*2)*18;
  7.     printf("offset = %d\n", offset);
  8.     if(hd == 1)
  9.     {
  10.         offset = (offset + 18);
  11.     }
  12.     printf("offset = %d\n", offset);
  13.     offset = (offset + sect);
  14.     printf("offset = %d\n", offset);
  15.     offset = (offset * 512);
  16.     printf("offset = %d\n", offset);
  17.     offset = (offset - 512);
  18.     printf("offset = %d\n", offset);
  19.     return(offset);
  20. }
Value of Sect: 3
Value of Cyl: 6
Value of Hd: 1

value that should be returned by this is 120832 but actual value is -10240
Thanks,
Dinklebaga
Feb 17 '09 #1
8 4696
donbock
2,426 Expert 2GB
@dinklebaga
The "%d" conversion specifier promises printf that you passed a signed int -- but you didn't!
I suggest you review the printf documentation to find the proper conversion specifier.
Feb 17 '09 #2
Savage
1,764 Expert 1GB
I don't see the return type of the function.Is it unsigned long too?
Feb 17 '09 #3
@Savage
Regardless of the return type I am not getting the correct result. Is there an issue using a 16 bit memory model with forcing unsigned long integers to be signed? Just to let you know that I am using the digital mars c/c++ compiler with the 16bit dos extender. no matter what I do with this compiler setup it always uses the 16th bit as the sign value despite casting them as unsigned. Have you ever encountered these issues before?
Regards,
Dinklebaga
Mar 6 '09 #4
JosAH
11,448 Expert 8TB
@dinklebaga
Read the very first reply in this thread: you are printing your unsigned long value as if it were a simple signed int (%d). You simply lied to the printf function. Check your manual how to print a long int and an unsigned int.

kind regards,

Jos
Mar 6 '09 #5
@JosAH
I have changed the printf statement to %u in accordance with the manual but it is still giving the wrong value even when explicitly declaring the result as 120832. It comes out as 55296
Mar 6 '09 #6
donbock
2,426 Expert 2GB
@dinklebaga
The value "512" is too large to fit in a char. You get integer overflow.
@dinklebaga
In the assignment to "offset", "cyl" is promoted to an int and the computation is performed on int's, then the result is cast to an unsigned long. If you are worried about overflow in the computation then you need to command the compiler to use long arithmetic:
Expand|Select|Wrap|Line Numbers
  1.     offset = ((unsigned long)cyl)*2*18;
Argument "cyl" has a signed type. What would you like to see happen if the caller passes a negative value? Why not declare all of the function arguments as "unsigned int" or "unsigned long" or "size_t"?

By the way, if sectorsize is truly a fixed constant, then it should be defined as follows (except change the type from char to something else):
Expand|Select|Wrap|Line Numbers
  1.     static const char sectorsize = 512;
Mar 6 '09 #7
donbock
2,426 Expert 2GB
@dinklebaga
%u is the conversion specifier for an "unsigned int". You want to print an "unsigned long". You need a slightly different conversion specifier.
Mar 6 '09 #8
JosAH
11,448 Expert 8TB
@dinklebaga
Try "%lu", the ell stands for "long" and the u stands for "unsigned".

kind regards,

Jos
Mar 6 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
11
by: Grant Edwards | last post by:
I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
3
by: Mike Miller | last post by:
What is the best way to convert a managed unsigned int64 to an unsigned long? Basically I need to do the following: System::UInt64 managedInt = 10; unsigned long unmanagedInt; unmanagedInt =...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
10
by: jeff | last post by:
unsigned long a = ...; long b = (long)a; while a overflows, what is the result of b? Thank you.
22
by: Vijay | last post by:
With the option strict On set..... Dim fs As FileStream = File.OpenRead(strFile) With fs Dim buffer(fs.Length) As Byte ' <--- Option Strict On disallows implicit conversions from 'Long' to...
8
by: wenmang | last post by:
Hi, I encountered a problem involving storage of unsigned long long. The requirement of incoming data field is an unsigned 64-bit integer, but on our system, due to lack of support of 64-bit...
6
by: CFAN | last post by:
Here is a example of double-type-conversion char * __cdecl _itoa ( int val, char *buf, int radix ) { if (radix == 10 && val < 0) xtoa((unsigned long)val, buf, radix, 1); else
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.