473,807 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string comparison: signed or unsigned char?

g
Hi all.
Maybe this question has been asked many times before, but I was not able to find
any pointer. I apologize in advance, for it refers to a particular standard
library implementation (GNU C++ version 3.x), but perhaps it's more general than
that.

The following program (to be compiled with g++ 3.x, 2.9x is not enough)

#include <iostream>
#include <string>
#define BLURB(x) #x << "\t== " << (x) << '\n'
int main()
{
std::basic_stri ng<char> s1, s2;
s1 = 0xe0;
s2 = 'a';
std::cout << "s1 == '" << s1 << "'\ns2 == '" << s2 << "'\n";
std::cout << BLURB( (s1 < s2) );
std::cout << BLURB( (s1[0] < s2[0]) );
std::cout << BLURB( std::char_trait s<char>::lt(s1[0], s2[0]) );
std::cout << BLURB( std::char_trait s<char>::compar e(s1.c_str(),
s2.c_str(), 1) );
std::cout << BLURB( s1.compare(s2) );
return 0;
}

produces this output on my x86 linux pc:

s1 == 'à'
s2 == 'a'
(s1 < s2) == 0
(s1[0] < s2[0]) == 1
std::char_trait s<char>::lt(s1[0], s2[0]) == 1
std::char_trait s<char>::compar e(s1.c_str(), s2.c_str(), 1) == 1
s1.compare(s2) == 1

The above results are counter-intuitive, but agree with the
behaviour of C standard library (1999 standard):
- strcmp and memcmp treat their arguments as unsigned char*, so
that strcmp(s1.c_str (), s2.c_str()) > 0 (meaning s1 > s2)
- on my platform char is signed, so s1[0] < s2[0] (because s1[0] < 0)

On the other hand, Stroustrup's TC++PL, section 20.2.1 "Character traits"
reports "The compare() function uses lt() and eq() to compare characters.",
so I'd expect
std::char_trait s<char>::lt(s1[0], s2[0])
and
std::char_trait s<char>::compar e(s1.c_str(), s2.c_str(), 1)
to return consistent results, which they do not. As a side effect,
s1.compare(s2) is not consistent with std::char_trait s<char>::lt()
either.

As far as I can see, GNU libstdc++5 implementation of
std::char_trait s<char>::compar e() uses memcmp() instead of lt(), and so
inherits the unsigned char comparison, while std::char_trait s<char>::lt()
plainly uses '<' to compare its arguments, keeping them signed.

It's quite likely that I am missing something in Stroustrup's book.
Does anybody know what the standard mandates?

giuseppe
Jul 22 '05 #1
0 2923

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7616
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
9
3961
by: c_beginner | last post by:
Dear group, I want to implement a solution to the following link: http://acmicpc-live-archive.uva.es/nuevoportal/data/p2006.pdf As a beginning I am trying to implement this little sample program which will delete the repeated strings in two set of strings.
2
5126
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 promoted to "signed int": signed char
14
24285
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
33
15592
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In fact, they may not even represent complete characters. At this point I think of text as simple binary blobs. What charset, character encoding and termination they use should not be exposed in the interface used to operate on them. But now I have...
6
11163
by: compcreator | last post by:
I have tried the following program. The problem is it is printing False I checked values for a and b but there is something wrong with the comparison. I thing it might be because of signed and unsigned conversion. Either b is upgraded to unsigned and the resulting value during comparison is 5 or a is downgraded to signed and the resulting value is lower than -1.
9
244
by: junky_fellow | last post by:
guys, I need to declare a pointer to a string. Which of the following is better and why? char *pStr; unsigned char *pStr; thanks for any help ...
15
1849
by: Cartoper | last post by:
There is one little static C library to manage the serial number and unlock key for my application. Today it is compiled with Microsoft VC6 and linked into both VC6 modules and VS2005 modules. It is not a lot of code, but obviously it is very important code that is used all over: VS2005: used in a C++/CLI module for .Net VS2005: Apache module VC6: DLL that is called by the installation program
13
2736
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
9720
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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
10112
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
6879
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
5546
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...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.