473,794 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use the "unsigned" representation of a number

Assuming I have this function (it's not complete):

template<typena me InputType, typename OutputIterator>
void to_utf8(InputTy pe input, OutputIterator result)
{
// trivial conversion
if (input < (1 << 7)) {
*result = input; ++result;
}else {
*result = ((input >6) | 0xc0); ++result;
*result = ((input & 0x3f) | 0x80); ++result;
}
}

Everything works fine if InputType is "unsigned" but it could not work if
InputType is "signed".
This problem can be generalized in every template function where a numbering
compare is applied.

template<typena me InputType>
bool is_valid_xml_ra nge(InputType input)
{
if ( ( input >= 0x20 ) && ( input <= 0xD7FF ) )
return true;
else
return false;
}

Is there a way to use always the "unsigned" representation, e.g.

template<typena me InputType>
bool is_valid_xml_ra nge(InputType input)
{
UnsignedInputTy pe uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}

Regards,
Fabio
May 14 '07 #1
8 1793
FabioAng a écrit :
Assuming I have this function (it's not complete):

template<typena me InputType, typename OutputIterator>
void to_utf8(InputTy pe input, OutputIterator result)
{
// trivial conversion
if (input < (1 << 7)) {
*result = input; ++result;
}else {
*result = ((input >6) | 0xc0); ++result;
*result = ((input & 0x3f) | 0x80); ++result;
}
}

Everything works fine if InputType is "unsigned" but it could not work if
InputType is "signed".
This problem can be generalized in every template function where a numbering
compare is applied.
[snip]

Is there a way to use always the "unsigned" representation, e.g.
[snip]
What you want to known is in fact if there is ones above 7th bit:
if( (input>>7) != 0 ) ...

Michael
May 14 '07 #2

"Michael DOUBEZ" <mi************ @free.frwrote in message
news:46******** *************** @news.free.fr.. .
>
What you want to known is in fact if there is ones above 7th bit:
if( (input>>7) != 0 ) ...

Michael
I would like to solve the generic problem not the only one I posted

Fabio
May 14 '07 #3
On May 14, 9:39 pm, "FabioAng" <fabioangNOS... @libero.itwrote :
"Michael DOUBEZ" <michael.dou... @free.frwrote in message

news:46******** *************** @news.free.fr.. .
What you want to known is in fact if there is ones above 7th bit:
if( (input>>7) != 0 ) ...
Michael

I would like to solve the generic problem not the only one I posted

Have you looked at this ?

http://www.unicode.org/Public/PROGRA...F/ConvertUTF.c
May 14 '07 #4

"FabioAng" <fa************ @libero.itwrote in message
news:f2******** **@nnrp-beta.newsland.i t...
Is there a way to use always the "unsigned" representation, e.g.

template<typena me InputType>
bool is_valid_xml_ra nge(InputType input)
{
UnsignedInputTy pe uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}
You can do it via template specialization

template<class Tstruct unsigned_type { typedef T type; };
template<struct unsigned_type<c har{ typedef unsigned char type; };
template<struct unsigned_type<s igned char{ typedef unsigned char
type; };
template<struct unsigned_type<s hort{ typedef unsigned short type; };
template<struct unsigned_type<i nt{ typedef unsigned int type; };
template<struct unsigned_type<l ong{ typedef unsigned long type; };
template<typena me InputType>
bool is_valid_xml_ra nge(InputType input)
{
typedef unsigned_type<I nputType>::type UnsignedInputTy pe;
UnsignedInputTy pe uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}
- Sylvester
May 14 '07 #5
I found a solution in an old boost thread

#include <boost/type_traits.hpp >
#include <boost/integer.hpp>

namespace detail
{
template <class T, bool is_signed>
struct remove_signedne ss_helper
{
typedef T type;
};

template <class T>
struct remove_signedne ss_helper<T, true>
{
typedef typename boost::uint_t<s izeof(T) * 8>::least type;
};
}

template <class T>
struct remove_signedne ss
{
typedef typename
detail::remove_ signedness_help er<T,boost::is_ signed<T>::valu e>::type type;
};

template<typena me InputType, typename OutputIterator>
void to_utf8(InputTy pe input, OutputIterator result)
{
remove_signedne ss<InputType>:: type uinput;
uinput = input;
// trivial conversion
if (uinput < (1 << 7)) {
*result = uinput; ++result;
} else {
*result = ((uinput >6) | 0xc0); ++result;
*result = ((uinput & 0x3f) | 0x80); ++result;
}
}

Is there anybody who sees something wrong in this piece if code ?

Regards
Fabio
May 14 '07 #6

"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:46******** *************@n ews.xs4all.nl.. .
>
"FabioAng" <fa************ @libero.itwrote in message
news:f2******** **@nnrp-beta.newsland.i t...
>Is there a way to use always the "unsigned" representation, e.g.

template<typen ame InputType>
bool is_valid_xml_ra nge(InputType input)
{
UnsignedInputTy pe uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}

You can do it via template specialization

template<class Tstruct unsigned_type { typedef T type; };
template<struct unsigned_type<c har{ typedef unsigned char type; };
template<struct unsigned_type<s igned char{ typedef unsigned char
type; };
template<struct unsigned_type<s hort{ typedef unsigned short type; };
template<struct unsigned_type<i nt{ typedef unsigned int type; };
template<struct unsigned_type<l ong{ typedef unsigned long type; };
template<typena me InputType>
bool is_valid_xml_ra nge(InputType input)
{
typedef unsigned_type<I nputType>::type UnsignedInputTy pe;
UnsignedInputTy pe uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}
- Sylvester
Great idea...the same which I found using boost library

Thanks
Fabio
May 14 '07 #7
On May 14, 5:35 am, "FabioAng" <fabioangNOS... @libero.itwrote :
if (input < (1 << 7)) {
if ( *(unsigned char *) &input < (1 << 7)) {

May 14 '07 #8
FabioAng wrote:
I found a solution in an old boost thread
....
Is there anybody who sees something wrong in this piece if code ?
It's wrong ? It only works for is8859-1 ?
May 18 '07 #9

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

Similar topics

43
3428
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
2
4203
by: jian | last post by:
Hi, I am looking at netinet/ip.h in linux code, and the declaration: unsigned int ip_hl:4 make me confused. I can understand "unsigned int ip_hl", but, what does ":4" here mean? Very grateful to any answer. Jian
4
10733
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
2
7320
by: runner | last post by:
I'm trying to call some functions from OpenSSL library but I'm a bit confused when I have to use pinvoke. first function should create key from some input data, it's declared : void BF_set_key(BF_KEY *key, int len, const unsigned char *data); I've translated it as public static extern IntPtr BF_set_key (IntPtr key, int len, byte data) and I try to call it this way:
10
3129
by: Kenneth Brody | last post by:
Does the cast "(unsigned)" imply "(unsigned int)", or does it simply strip the signedness from the variable? In other words, given this: long l = -123; unsigned long ul = (unsigned)l; Does the value of "l" get demoted to an unsigned int during the assignment, or does it remain a long? (I am on a system where
0
9672
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
9519
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,...
0
10435
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
10213
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...
0
10000
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...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
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
5436
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
3721
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.