Message Boards Message Boards

0
|
615 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Base converter for strings

Posted 3 months ago

I would like to convert a text string to an integer. Is there a way that I can do this in Mathematica without manually typing the polynomial function for each of the string’s characters? I know that there is the BaseForm function; however, that is limited to bases between 2 and 36. For what I have, I need arbitrary values. I have 4 structures to convert strings to decimal numbers. One converts the strings from bases that are powers of 2. The remaining 3 convert the string from arbitrary bases up to 16,777,216. I figured out how to start with an arbitrary base that is determined by the highest character code in the string. What I don’t know how to do is use the raw string as input and have it converted to decimal in place of the string’s polynomial function. Is there a way that I can replace string polynomials with the raw strings, while retaining my intended functionality? I have attached a notebook to showcase what I am currently doing and would like to do in a more efficient manner.

Attachments:
POSTED BY: Caleb Stevens
2 Replies
Posted 3 months ago

That worked. Thank you, Bill.

POSTED BY: Caleb Stevens
Posted 3 months ago

This

ToCharacterCode["123abc"]

returns

{49,50,51,97,98,99}

and

Fold[256*#1+#2&,0,ToCharacterCode["123abc"]]

returns

54091680146019

I got all that by wandering around the help pages with a few lucky guesses.

If the size of the numbers, within reason, do not really matter then perhaps that does what you need, as long as you don't include things like Greek characters and other special characters that have a character code greater than 255.

Or if you really need to compress the range of the result then perhaps you could write a simple Piecewise function that would be mapped onto the output of ToCharacterCode and would reduce the range of character codes down to something you like better.

POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract