2015-11-30 3 views
-2

Я прочитал огромный двоичный файл в векторе char s.Преобразование char в беззнаковое целое

Мне нужно обрабатывать каждый байт как целое без знака (от 0 до 255); и сделать некоторую арифметику. Как преобразовать вектор в вектор?


char a = 227; 
cout << a; 

печатает?


char a = 227; 
int b = (int) a; 
cout << b << endl; 

печатает -29


char a = 227; 
unsigned int b = (unsigned int) a; 
cout << b << endl; 

печатает 4294967267


char a = 227; 
unsigned char b = (unsigned char) a; 
cout << b << endl; 

печатает?

+4

'unsigned char'? – twentylemon

+0

У меня уже есть этот вектор . когда я конвертирую 'char' в' unsigned char', он все еще печатает? – mustafa

+0

char -> unsigned char -> int/unsigned – Kevin

ответ

0
std::vector<char> source; 
// ... read values into source ... 

// Make a copy of source with the chars converted to unsigned chars. 
std::vector<unsigned char> destination; 
for (const auto s : source) { 
    destination.push_back(static_cast<unsigned char>(s)) 
} 

// Examine the values in the new vector. We cast them to int to get 
// the output stream to format it as a number rather than a character. 
for (const auto d : destination) { 
    std::cout << static_cast<int>(d) << std::endl; 
} 
Смежные вопросы