473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 2553
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Roger | last post by:
How can I pass parameters to a style sheet? I have noticed a couple of sites are now passing variables to the style sheet, which appear to be substituted at run time. For example: <link...
22
by: Alan Silver | last post by:
Hello, I am updating some old web pages, and have come across a problem. I have some tables where the opening tag looks like... <table border="1" bordercolor="#ffffff"> which gives a nice...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.