473,385 Members | 1,834 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,385 software developers and data experts.

How to use the "unsigned" representation of a number

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

template<typename InputType, typename OutputIterator>
void to_utf8(InputType 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<typename InputType>
bool is_valid_xml_range(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<typename InputType>
bool is_valid_xml_range(InputType input)
{
UnsignedInputType uinput = input;

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

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

template<typename InputType, typename OutputIterator>
void to_utf8(InputType 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.it...
Is there a way to use always the "unsigned" representation, e.g.

template<typename InputType>
bool is_valid_xml_range(InputType input)
{
UnsignedInputType 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<char{ typedef unsigned char type; };
template<struct unsigned_type<signed char{ typedef unsigned char
type; };
template<struct unsigned_type<short{ typedef unsigned short type; };
template<struct unsigned_type<int{ typedef unsigned int type; };
template<struct unsigned_type<long{ typedef unsigned long type; };
template<typename InputType>
bool is_valid_xml_range(InputType input)
{
typedef unsigned_type<InputType>::type UnsignedInputType;
UnsignedInputType 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_signedness_helper
{
typedef T type;
};

template <class T>
struct remove_signedness_helper<T, true>
{
typedef typename boost::uint_t<sizeof(T) * 8>::least type;
};
}

template <class T>
struct remove_signedness
{
typedef typename
detail::remove_signedness_helper<T,boost::is_signe d<T>::value>::type type;
};

template<typename InputType, typename OutputIterator>
void to_utf8(InputType input, OutputIterator result)
{
remove_signedness<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.nlwrote in message
news:46*********************@news.xs4all.nl...
>
"FabioAng" <fa************@libero.itwrote in message
news:f2**********@nnrp-beta.newsland.it...
>Is there a way to use always the "unsigned" representation, e.g.

template<typename InputType>
bool is_valid_xml_range(InputType input)
{
UnsignedInputType 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<char{ typedef unsigned char type; };
template<struct unsigned_type<signed char{ typedef unsigned char
type; };
template<struct unsigned_type<short{ typedef unsigned short type; };
template<struct unsigned_type<int{ typedef unsigned int type; };
template<struct unsigned_type<long{ typedef unsigned long type; };
template<typename InputType>
bool is_valid_xml_range(InputType input)
{
typedef unsigned_type<InputType>::type UnsignedInputType;
UnsignedInputType 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
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
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...
4
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...
2
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...
10
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.