Oops! (Addition in line)
Hal Vaughan wrote:
> Mike Wahler wrote:
>
>>
>> "Hal Vaughan" <hal@[EMAIL PROTECTED]
> wrote in message
>> news:0h4Cj.11565$wM2.6305@[EMAIL PROTECTED]
>>>I need to store 2 - 4 values that I have in hex in a map. I've tried
>>> storing an int[] in a map and can't make it work.
>>
>> Arrays can't be stored in standard library containers.
>> They're not copy-constructible or assignable.
>>
>>> As of now, the only way
>>> I can think of is to store it as a string, like "0x01 0xDF". But then
>>> when
>>> I get it out of the map, I don't see how to convert it to one int for
>>> each specified hex number.
>>
>> #include <ios>
>> #include <iostream>
>> #include <sstream>
>> #include <string>
>>
>> int main()
>> {
>> std::string s("0x01 0xDF");
>> std::istringstream iss(s);
>> int a = 0;
>> int b = 0;
>> iss >> std::hex >> a >> b;
>> std::cout << a << ' ' << b << '\n';
>> return 0;
>> }
>
> Okay, if I have s as "0x31 0x5A" first (I'm making it easy for me with
> these
> numbers), I'll need them as ints first, so that helps. I thought I had,
> while looking up conversion info, also found a way, once I got the hex
to
> int, to convert it to a char. In this case, I'd have two chars, the
first
> is '1' and the second is 'Z'. Once I have the int, is there a way to
get
> the char that number represents?
I should add that if I redo your code like this:
#include <ios>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string s("0x01 0xDF");
std::istringstream iss(s);
int a = 0;
int b = 0;
iss >> std::hex >> a >> b;
char c = a, d = b;
std::cout << a << ' ' << b << '\n';
std::cout << c << ' ' << d << std::endl;
return 0;
}
I know it works, but I've read that there is a problem with setting a char
directly equal to an int. I know, in this case, that ever hex number
represented in the string will be a one byte number so it can be
represented as a single char, so I'm not worried about any kind of
overflow, but are there other issues to worry about?
Thanks again!
Hal


|