O
Odysseas Gabrielides
How can I use unsigned chars in a string object ?
How can I use unsigned chars in a string object ?
The portable way is by specialising on unsigned char
, and some
compilers allow you to define the default char as unsigned (although
this formally doesn't make it of type unsigned char).
Vladyslav said:typedef std::basic_string< unsigned char, std::char_traits<unsigned
int>, std::allocator<unsigned char> > ustring;
Microsoft C++ compiler (/J option) - http://msdn.microsoft.com/en-us/library/0d294k5z(VS.80).aspx
GCC (-funsigned-char option) - http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/C-Dialect-Options.html
Others - http://www.google.com
Specializing what?
'Fraid not. The standard only specifies char_traits specializations for
char and wchar_t. There may not be any primary template definition.
char_traits<unsigned int>?
Why not simply
typedef std::basic_string<unsigned char> ustring;
?
V
Yes. You should obviously specialise std::basic_string on unsignedSpecializing what?
'Fraid not. The standard only specifies char_traits specializations for
char and wchar_t. There may not be any primary template definition.
Odysseas said:How can I use unsigned chars in a string object ?
To explain why I want to use unsigned chars:Odysseas said:How can I use unsigned chars in a string object ?
The portable way is by specialising on unsigned char,
typedef std::basic_string< unsigned char, std::char_traits<unsigned
int>, std::allocator<unsigned char> > ustring;
char_traits<unsigned int>?
Why not simply
typedef std::basic_string<unsigned char> ustring;
Not that huge. Five typedefs, fourteen simple (trivial?)
member functions.
Odysseas said:How can I use unsigned chars in a string object ?
To explain why I want to use unsigned chars:
I want to encode int and double with characters.
For exemple, to send via sockets the number 145. Instead of sending
'1','4' and '5' I thought of sending a string representing the number.
If I use char, the base will be 127. What do I mean ? I encode by doing:
buffer[0]=18 and buffer[1]=1. To decode the number, I do:
number+=buffer*pow(127,i)
Well I wanted to use unsigned char to be able to increase the numerical
base, and encode larger numbers (base with unsigned char -> 255 instead
of 127)
Exemple:
With 1 char I encode: 127*1 Total: 127 numbers
With 2 char I encode: 127*2 Total: 16129 numbers
With 3 char I encode: 127*3 Total: 2048383 numbers
With 1 unsigned char I encode: 255*1 Total: 255 numbers
With 2 unsigned char I encode: 127*2 Total: 65025 numbers
With 3 unsiged char I encode: 127*3 Total: 16581375 numbers
See the difference ?
So I was just wonderinf if I could use string objects or if I had to use
old char arrays to preparethe data for sending.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.