![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Re: Binary/Hex Text Encoder/Decoder
You seem to be going about this in an unnecessarily complicated fashion. Have you considered using
strtol() and/or strtoul() from <cstdlib>? Using one of these functions will allow you to select the base (anything from 2 to 36), and also detect strings that don't convert, or only partially convert (do this by comparing start and end).If you're wanting to write your own method as a learning experience (which really seems to be the only reasonable explanation for reinventing the wheel), then it's fairly simple. As a generalized case, you check each digit to see if it's in the range for the selected base. If it is, you convert and store it, then move on to the next digit. Repeat until you've converted as much of the string as possible (this might mean hitting the string's end, or a digit that won't convert- whether this is an error or not is up to you to determine). Upon completing the conversion of each digit, you will be left with a collection of zero or more values. By counting these, you know how many digits were converted, and by extension, what magnitude each value is. For example, imagine I'm converting the string "1357" to decimal. The first digit converts, giving me the value 1, and the next gives 3, and so on. When I'm done, I have five values; thus I know the first value, 1, refers to thousands, the second to hundreds, and so forth. You can hold these values in a collection; if it's a fixed-size collection such as an array, you can size it depending on the base and size of the number ( int, long, etc). From there, it's a simple matter to convert the stored values into your final value by scaling them according to their magnitude and summing them. If you have too many values, or the highest-order value is too large, then you have an overflow condition. Whether you handle this by truncating the final value or treating it as an error is again up to you.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#12 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: Binary/Hex Text Encoder/Decoder
>I'd be glad to accept one as soon as I actually see one posted here.
I don't believe you. My answer was "professional", and probably a great deal better than any manual process you're likely to come up with. I don't like being ignored, so if you're discarding my help for any particular reason, I'd like to hear it.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#13 |
|
Retired Programmer
Join Date: Jul 2006
Posts: 45
Rep Power: 0
![]() |
Re: Binary/Hex Text Encoder/Decoder
Narue, I am not using your example because it uses std::string. When I use wxWidgets I prefer to use wxString. Yes, your example is great; I am doing this for practice, as in my age if I stop programming for awhile, I begin to forget how to do certain things. If I wanted to do this the easy way I could use this method:
The wxString class has a member function called ToLong(long *val, int base). You specify a pointer to a variable that will recieve the result val. Then you specify a base. In my case I'd use 16 or 2 for binary or hex. I prefer to do this the long way for practice. I've created converters in other systems in different programming languages. Although it was easier because I was doing it in assembly. |
|
|
|
|
|
#14 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Binary/Hex Text Encoder/Decoder
Quote:
Quote:
![]()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#15 |
|
Sexy Programmer
|
Re: Binary/Hex Text Encoder/Decoder
Can't you just use Narue's solution and then find a way to convert the std::string to a wxString?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#16 |
|
Retired Programmer
Join Date: Jul 2006
Posts: 45
Rep Power: 0
![]() |
Re: Binary/Hex Text Encoder/Decoder
Why? The same same thing can be done easily in wxWidgets with identical functions that are meant for wxString. There is no reason to convert to std::string. Besides, I have created two version of the source. One is the short, more readable way of converting the other is the more complex route. Its important to learn how to do these things the long way, so when you don't have the luxuary of doing the short way, you'll be able to still complete the project.
David: Yes, I can still program it either way. Learning more than one way to solve a problem helps build quick problem solving skills. Narue's method is somewhat hazardous only because it uses std::string. I've had conversion problems in the past when converting from std::string to wxString. Besides, it doesn't really make sense to convert when identical code can be created with wxString functions. It even suggests in the wxWidgets documentation to avoid conversion and use wxString whenever possible. Nonetheless, Narue has one way of solving the problem. But I have two algorithms already so, I see no reason why this thread needs to be disscussed any longer. If you wish to argue further, take David's advice and write your reply in a notepad and send it to /dev/null. Last edited by Baphomet; Nov 8th, 2007 at 10:28 AM. |
|
|
|
|
|
#17 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
Re: Binary/Hex Text Encoder/Decoder
>I prefer to do this the long way for practice.
Then you don't want a professional answer. You want a naive algorithm. A professional answer makes proper use of the available tools to solve the problem in a productive manner. Doing it the long way doesn't have that attribute because it's more work for you (ie. unproductive). Don't ask for a professional answer when you really want a student answer. You're unlikely to get the code you want because unless the professional answer is coming from a student who thinks his answer is professional, you're going to get something that a real professional would write for real projects. It's also not a good idea to expect answers in the form of your non-portable library. We'll give you something portable and you can bastardize it into wxWidgets on your own. So I'm not going to use wxString. I'll continue to use std::string and you can perform that conversion on your own. The logic for a naive algorithm won't change. However, because I don't like your attitude, I think I'll refuse to help you further.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#18 |
|
Sexy Programmer
|
Re: Binary/Hex Text Encoder/Decoder
And if you wish to seek further help in anything else in the future, I'll tell you to take it and stick it up your ass!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding more info to a text file without erasing | crawforddavid2006 | C# | 2 | Apr 11th, 2007 2:10 PM |
| [perl] graphical text editor | glimmy | Existing Project Development | 0 | Aug 6th, 2006 11:47 PM |
| How to detect cursor location and insert text??? | syntax-error | C# | 3 | Jun 30th, 2005 1:42 AM |
| Printing rich text with colored background | jarrod | C# | 1 | Feb 23rd, 2005 11:48 AM |