Function

Help in writing a function that checks from a string the the following: Binary, Hex and Decimal.

bool is bin(String,num){


return is binary
}

bool is hex(String,num){


return is hex
}

bool is decimal(String,num){


return is decimal
}

How does that help us to write such functions?

In other words: do you have a question/issue/problem/error?
I just wanted some assistance is writing the function because i am new in the programming world
Hello scott14,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


You have a start for three different functions. As long as the returned value is either "true" or "false" it will work. What is in the () is a bit lacking for a function. It is OK for a prototype though.

Now all you need is something between the opening { and the return statement.

The question is are you just checking what type of number it is or do you mean to return a converted number?

This may help:
http://www.cplusplus.com/doc/tutorial/functions/

Hope that helps,

Andy
The easy way to do this is with strings.
you can drop an integer into a string in various formats, including hex. That gives you the hex answer straight up.

hex is one to one with binary, based off nibbles.
that is
0x1F
1 is a nibble, and F is a nibble.
1 is 0001 and f is 1111 so the full byte is
00011111
and that scales for any size of integer.
you can use a lookup table of strings for the nibbles so your conversion might look like
resultstring += binarytable[mynumberasstring[i]];
where it peels off a hex digit from the string, looks up the binary for it, and builds the answer.

There are other ways to do it (dozens) but this one is easy to code.
Last edited on
Thousand Thanks @ Handy Andy (2439) and jonnin (3612). Bravo!!
Topic archived. No new replies allowed.