Sunday, 15 September 2013

basic_string of unsigned char Value Type

basic_string of unsigned char Value Type

So, string comes with the value type of char. I want a string of value
type unsigned char. Why i want such a thing is because i am currently
writing a program which converts large input of hexadecimal to decimal,
and i am using strings to calculate the result. But the range of char,
which is -128 to 127 is too small, unsigned char with range 0 to 255 would
work perfectly instead. Consider this code:
#include<iostream>
using namespace std;
int main()
{
typedef basic_string<unsigned char> u_string;
u_string x= "Hello!";
return 0;
}
But when i try to compile, it shows 2 errors, one is _invalid conversion
from const char* to unsigned const char*_ and the other is initializing
argument 1 of std::basic_string<_CharT, _Traits,
_Alloc>::basic_string...(it goes on)
EDIT: "Why does the problem "converts large input of hexadecimal to
decimal" require initializing a u_string with a string literal?" While
calculating, each time i shift to the left of the hexadecimal number, i
multiply by 16. At most the result is going to be 16x9=144, which
surpasses the limit of 127, and it makes it negative value.
So, what should i do?

No comments:

Post a Comment