Re: char[] = "FFFFFF" to 3 unsigned char.
From: Unforgiven (jaapd3000_at_hotmail.com)
Date: 05/26/04
- Next message: Leor Zolman: "Re: pass an array throuh a function"
- Previous message: Kenny: "pass an array throuh a function"
- In reply to: ckroom: "char[] = "FFFFFF" to 3 unsigned char."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 May 2004 00:37:36 +0200
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
- Next message: Leor Zolman: "Re: pass an array throuh a function"
- Previous message: Kenny: "pass an array throuh a function"
- In reply to: ckroom: "char[] = "FFFFFF" to 3 unsigned char."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|