473,411 Members | 2,030 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,411 software developers and data experts.

comparison is always true due to limited range of data type BUT ITS NOT ALWAYS TRUE

smartCryptor.cpp: In function `void Char2Hex(unsigned char, char*)':

smartCryptor.cpp:188: warning: comparison is always true due to limited range of data type



Expand|Select|Wrap|Line Numbers
  1. void Char2Hex(unsigned char ch, char* szHex)
  2. {
  3.  
  4.    unsigned char byte[2];
  5.    byte[0] = ch/16;
  6.    byte[1] = ch%16;
  7.  
  8.    for(int i=0; i<2; i++)
  9.    {
  10.       if(byte[i] >= 0 && byte[i] <= 9)
  11.          szHex[i] = '0' + byte[i];
  12.       else
  13.          szHex[i] = 'A' + byte[i] - 10;
  14.    }
  15.  
  16.    szHex[2] = 0;
  17.  
  18.    }
may somebody explain why compiler gives this even though its not always true. or am i missing something?

thanks,
Aldrich
May 18 '07 #1
2 12149
Ganon11
3,652 Expert 2GB
In your if statement, you write:

Expand|Select|Wrap|Line Numbers
  1. if (byte[i] >= 0 && byte[i] <= 9)
but perhaps you should be comparing with the characters '0' and '9' rather than the values 0 and 9:

Expand|Select|Wrap|Line Numbers
  1. if (byte[i] >= '0' && byte[i] <= '9')
May 18 '07 #2
AdrianH
1,251 Expert 1GB
In your if statement, you write:

Expand|Select|Wrap|Line Numbers
  1. if (byte[i] >= 0 && byte[i] <= 9)
but perhaps you should be comparing with the characters '0' and '9' rather than the values 0 and 9:

Expand|Select|Wrap|Line Numbers
  1. if (byte[i] >= '0' && byte[i] <= '9')
No, what the OP posted is correct. This has to do with the byte being unsigned. An unsigned variable cannot be less than zero. Thus your expression byte[i] >= 0 will always be true.

Signed and unsigned values mess up a lot of people. I'm not special, I've just had a lot of exposure with them. Had to write a library that did things like sign extention and not depending on the type it was assigned in a C DB, just because of this reason (long story).


Adrian
May 18 '07 #3

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

Similar topics

5
by: democratix | last post by:
Hi, I've only got a couple years experience developing for Access but have recently been experimenting with HTML/javascript for gui and client-side scripting, mysql for database and php for...
5
by: Simon Burton | last post by:
In today's experiment, I was wondering if I could make the comparison operators (<,<=,>=,>) work on classes (types) according to inheritance. The idea is, for example, classes lower in the class...
133
by: jonathan | last post by:
hey all, I realize that this question might pop up from time to time, but I haven't seen it a while and things might of changed, so - Right now (July 2004) how does mysql stand up in...
2
by: Dave | last post by:
hello... I wrote a marco for saturation. #define clip(x) (char)(x)<0?0:((x)>255?255:(x)); and use this marco in the program like this... char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2)); ...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
4
by: Mike Duffy | last post by:
I just recently realized that the comparison operator "is" actually works for comparing numeric values. Now, I know that its intended use is for testing object identity, but I have used it for a...
2
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
8
by: John Ratliff | last post by:
"comparison is always false due to limited range of data type" I get this warning with g++ when I compile some code and I'm not quite sure I understand it. I have a small file that I've read...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...
0
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,...
0
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
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...

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.