tags:

views:

262

answers:

3

hi, I am basically trying to get a user to input a hexadecimal input via getline into a string as i will do other operations on this. (using c++ .net stuff won't work)

i do not want to break this into chars per say and then go through each char in the string and see if its in range from [0-9] or [Aa-Ff].

Instead, I wanted to know if there was a cool function that anyone knew of or a better way to do it. I am aware of the strtoul function but it returns a long. this will force me to then i guess pass it to a stream to make it back into a string again.

another thing with the long i am not sure of if i have to worry about 64 bit long vs 32 bit long. I am developing this on a linx box using an intel processor but it could be used on a unix box whose processor could be 64 bit i am not sure.

so i guess there are two questions here really. any help would be most welcome

could I get an answer on: can you also comment on my second question about the long? even though i don't have to worry about that now...if i save a variable in a long using a 32 bit system....would that change ( i imagine so the size of long should change on a 64 bit processor) what would this mean for the info saved in the variable? and second in order to avoid the whole little/big endian thing i saved it in a long thinking since its a register of sorts it would not be an issue with porting. was i wrong to think that?

thanks

A: 
int val;
scanf("%x", &val);

? (Won't check if the whole line is valid.)

KennyTM
This doesn't come close to doing what he asked. It fails if the input is 0. If you give it a partial hexadecimal string, it will return the valid part, but not alert him that the rest is invalid.
dauphic
-1. This is a code fragment that doesn't even answer the question (what do you do after calling `scanf`? There's no check that it matched *anything*), and `scanf` is simply bad (people should prefer using `sscanf`, although that would be a poor solution too for the reasons dauphic mentioned).
jamesdlin
I understand this doesn't completely solve the problem, but the criticism is getting ridiculous. (1) It doesn't fail if the input is 0. 0 is a valid hex number. (2) Prefer using `sscanf` over `scanf`? `sscanf` parses from a `char*`, `scanf` parses from `stdin`, how can you compare the two?
KennyTM
(1) I think dauphic meant it fails if there's no input, not if someone literally entered the string "0". (2) *Never* use `scanf`. http://c-faq.com/stdio/scanfprobs.html
jamesdlin
+2  A: 

Checking each char is the only way it can be done, period.

However, you may be interested in isxdigit(int character) which returns 0 if the character passed isn't a valid hexadecimal character (note that x is not included as a valid character).

You can test if it's a hex string in a single line using algorithms, though it's a bit ugly. If you're using Boost, you can pretty it up a lot by using boost::bind.

The headers required by this snippet are <locale>, <functional>, and <algorithm>.

bool is_hex_string(std::string& str) {
  return std::count_if(str.begin(), str.end(), 
    std::not1(std::ptr_fun((int(*)(int))std::isxdigit))) > 0;
}
dauphic
i liked the isxdigit function. I did a quick function thingybool inpChk (string inputString){ for (int i=0;i<inputString.size();i++) if(!isxdigit(inputString[i]) { std::cout<<"erro"<<std::endl; return false } else return true; }it seems to work fine.one question can you tell me what this casting you are using?int(*)(int)thanks
djones2010
There's almost never just one way to do something in C++.
John Dibling
`int(*)(int)` means a pointer to a function which takes an `int` and returns an `int`.
KennyTM
isxdigit is a template function, so it needs to be told exactly which template to use. int(*)(int) is the type of a function that takes an int as it's only parameter and returns an int, i.e. int func(int)
dauphic
sweet thanks. could i also get a comment on my second question thanks
djones2010
Your second question isn't very clear. Unless you're doing crazy operations on the long (i.e. bit twiddling, memcpy'ing, etc.) you shouldn't have to worry about it. If you're just assigning to it or doing arithmetic on it, it's size isn't a problem.
dauphic
A: 

if (sscanf(string, "%x", &val) != 1) // handle your error here

zvrba