lexical_cast<unsigned char>: Compiler error

Hello, I have a question. I need a lexical_cast for a BYTE. Now I get allways a compile error with VS2005 and VS2008: e.g. unsigned char test = 2; test = boost::lexical_cast<unsigned char>(L"2"); Error 2 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'unsigned char' (or there is no acceptable conversion) D:\boost\boost_1_38_0\boost\lexical_cast.hpp 771 What I do wrong? How I can solve this problem? Regards Hansjörg

Hi!
strange code... You are trying to convert a 2 byte character string into a
single byte unsigned character value? Std C++ does not definitely provide
possibility for it. But if you know how to do the conversion, overload the
operator >>, which accepsts the istream and unsigned char, smth. similar to:
namespace your_namespace //define it in your namespace, but so that ADL
finds it
{
std::istream& operator>>(std::istream& is, unsigned char& ch)
{
// read needed ammount of bytes from is (actually 1)
ch=// convert it and assign to ch...
return is;
}
}
Try using wchar_t instead of unsigned char.
Regards,
Ovanes
On Fri, Mar 6, 2009 at 4:04 PM, Hansi
Hello,
I have a question. I need a lexical_cast for a BYTE. Now I get allways a compile error with VS2005 and VS2008:
e.g.
unsigned char test = 2;
test = boost::lexical_cast<unsigned char>(L"2");
Error 2 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'unsigned char' (or there is no acceptable conversion) D:\boost\boost_1_38_0\boost\lexical_cast.hpp 771
What I do wrong? How I can solve this problem?
Regards Hansjörg

Ovanes Markarian schrieb:
Hi!
strange code... You are trying to convert a 2 byte character string into a single byte unsigned character value? Std C++ does not definitely provide possibility for it. But if you know how to do the conversion, overload the operator >>, which accepsts the istream and unsigned char, smth. similar to:
The code is not so strange because for me the unsigned char is not a character but a number...

On Fri, Mar 6, 2009 at 4:45 PM, Hansi
Ovanes Markarian schrieb:
Hi!
strange code... You are trying to convert a 2 byte character string into a single byte unsigned character value? Std C++ does not definitely provide possibility for it. But if you know how to do the conversion, overload the operator >>, which accepsts the istream and unsigned char, smth. similar to:
The code is not so strange because for me the unsigned char is not a character but a number...
IMO this is strange, because you cut from the number a part, which might contain useful data and C++ prevents you from that. Why don't you convert it to wchar_t or unsigned short? L"..." produces multi byte char string. Regards, Ovanes

haracter but a number...
IMO this is strange, because you cut from the number a part, which might contain useful data and C++ prevents you from that. Why don't you convert it to wchar_t or unsigned short? L"..." produces multi byte char string.
Maybe it is hard to understand,but I don't need the character or wchar_t but for me the string represents a number...the number has nothing to do with the character but signed char and unsigned char is the only representation for 8bit numbers in c++...for that I don't cut anything if the number does not exceeds the limits of a 8bit number... Regards Hansjörg

Hi!
On Sun, Mar 8, 2009 at 5:50 PM, Hansi
Maybe it is hard to understand,but I don't need the character or wchar_t but for me the string represents a number...the number has nothing to do with the character but signed char and unsigned char is the only representation for 8bit numbers in c++...for that I don't cut anything if the number does not exceeds the limits of a 8bit number...
May be I don't get it. Just to be sure: Which type is produced by L"..." macro? AFAIK const wchar_t[]. My question was: why to use L-macro and produce multi-byte string if you require single byte string anyway? Thanks, Ovanes

May be I don't get it. Just to be sure: Which type is produced by L"..." macro? AFAIK const wchar_t[]. My question was: why to use L-macro and produce multi-byte string if you require single byte string anyway?
If it is a input from a user and you have to convert it to a number...

On Fri, Mar 6, 2009 at 10:04, Hansi
I have a question. I need a lexical_cast for a BYTE. Now I get allways a compile error with VS2005 and VS2008:
e.g.
unsigned char test = 2;
test = boost::lexical_cast<unsigned char>(L"2");
Error 2 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'unsigned char' (or there is no acceptable conversion) D:\boost\boost_1_38_0\boost\lexical_cast.hpp 771
What I do wrong? How I can solve this problem?
You want to get '\002', not '2', right? If so, then it sounds like you want the same solution as for the first problem in the FAQ: http://www.boost.org/doc/libs/1_38_0/libs/conversion/lexical_cast.htm#faq

Scott McMurray schrieb:
On Fri, Mar 6, 2009 at 10:04, Hansi
wrote: I have a question. I need a lexical_cast for a BYTE. Now I get allways a compile error with VS2005 and VS2008:
e.g.
unsigned char test = 2;
test = boost::lexical_cast<unsigned char>(L"2");
Error 2 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'unsigned char' (or there is no acceptable conversion) D:\boost\boost_1_38_0\boost\lexical_cast.hpp 771
What I do wrong? How I can solve this problem?
You want to get '\002', not '2', right? If so, then it sounds like you want the same solution as for the first problem in the FAQ: http://www.boost.org/doc/libs/1_38_0/libs/conversion/lexical_cast.htm#faq
you are right this is the answer...thanks...I think I have to make some workaround because I need it in a library where I can have different types...also 8bit...
participants (3)
-
Hansi
-
Ovanes Markarian
-
Scott McMurray