I just finished writing a string class that includes functions for generating random string (why I don't know, as I have never really needed random strings before). Basically the user can pass the function an enum citing what kind of string that'd like (i.e. AllUpperCase or AllNumeric). There is also an option for generating random hexadecimal, octal, and binary string representations. As it is, hex always begins with "0x", and binary with "0b", but I'm not sure what I should do for octals. I know the C/C++ compiler treats any number literal beginning with a 0 as an octal, but I always thought that was too confusing, as it is also a valid looking decimal number. Wikipedia suggest a solution to this is to use "0o" or "0q" in front of the number to distinguish it as an octal, and I was just wondering what other people's opinions were.
Recommended Answers
Jump to PostWell if the string class is C++ then I would adopt its convention..
Jump to PostI had that thought as well, but then my overwhelming hatred of the C++ syntax for it kicked in.
Then why are you coding in C++?
Jump to Postwhat are you trying to do with the octals?
Jump to PostWhy don't you seperate the randomString(HexOrDecOrOctal) function from the class, and
make it a regular function. Like so,MyString genRandomString(int radix){ validate(radix); //... }
No need for enums or IDs, just pass in the base, and the function returns something
that has to do with that base.
Jump to Post>I was just wondering what other people's opinions were.
I wouldn't bother with any kind of marker in the string. The caller clearly knows what radix they want to generate, so generate the string and let them add a prefix (or suffix) if they so desire.
All 17 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.