By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

char[] = "FFFFFF" to 3 unsigned char.

P: n/a
Hi, I have a vector of chars: "FFFFFF"
Each 2 chars from the vector in hex represents a decimal number, how can I
convert the vector to 3 decimal values (char *)"FF"->(int)256. I have no
idea.

Thanks!

--
ckroom
http://nazaries.net/~ckroom

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ckroom wrote:
Hi, I have a vector of chars: "FFFFFF"
Each 2 chars from the vector in hex represents a decimal number, how
can I convert the vector to 3 decimal values (char *)"FF"->(int)256.
I have no idea.


I used the hex value "abcdef" because it demonstrates the meaning more
clearly.
-----

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

int main()
{
std::string s = "abcdef";
std::istringstream iss(s);
unsigned int n;
iss >> std::hex >> n;
std::vector<unsigned int> result;
while( n > 0 )
{
result.push_back(n & 0xFF);
n >>= 8;
}

std::cout << std::hex;
std::copy(result.begin(), result.end(), std::ostream_iterator<unsigned
int>(std::cout, "\n"));

return 0;
}
-----

After this result[0] contains 0xEF, result[1] contains 0xCD and result[2]
contains 0xAB.

So the output is:
ef
cd
ab

Hope this helps you.

--
Unforgiven

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.