Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:38:09+00:00 2026-06-17T08:38:09+00:00

Note: I found a similar question , but it is in python. I’ve been

  • 0

Note: I found a similar question, but it is in python.


I’ve been trying to think of an algorithm or native method for this, and I’m stumped.

This is what I have so far:

encode=function(n,f){
    return (n).toString(f)
}
decode=function(s,f){
    return parseInt(s,f)
}

Basically, I need a way to convert a string, like 'Hello World!' to a base 10 (hexadecimal will work as well) number, like 14438792758793754875, and I am wondering if there’s a proper way to do it before I possibly waste my time with something like:

str='Hello World'
returnString=''
for(var i in str){
    returnString+=(
        str[i]=='A'?'0'
        :str[i]=='B'?'1'
        etc...
    )
}

Not only would that be very time consuming, but I have no idea how I would possibly convert it back, as once I get into numbers like 11 for L, a for loop wouldn’t do, and would return BB.

The reason I am asking this is because I plan to use it in the future for more efficient and secure, encrypted storage. for example, something like:

//save
numstr=encodeToNumber(inputElement.value)
b36str=encode(numstr,36)
storage['items'].push(b36str)
localStorage['App Data']=JSON.stringify(storage)

//load
if(localStorage['App Data']){
    storage=JSON.parse(localStorage['App Data'])
}else{
    storage={
        items:[],
        config:{
            etc
        }
    }
}
//Assuming storage.items looks like this: ['rgie', 'zyy', '2il7']
for(i in storage['items']){
    b36str=storage['items']//rgie, zyy, 2il7
    numstr=decode(b36str,36)//1281110, 46618, 117403
    container.innerHTML+='<hr>'+decodeFromNumber(numstr)//Milk, Eggs, Bread
}

//Output
Milk
Eggs
Bread

I actually did spend several minutes manually encrypting ‘Milk’, ‘Eggs’, and ‘Bread’ to their base36 counterparts so it could be accurate.


Update: Aadit M Shaw gave a function that produces hexadecimal strings from strings, and I reverse engineered it to my needs and got this:

en=function(s){
    var s2d=function(s){
        s=s.replace(/ /g,'+').replace(/!/g,'.')
        var n=[]
        for(var i in s){
            n.push(s.charCodeAt(i))
        }
        return n
    }
    var arr=s2d(s)
    var s2=''
    for (var i in arr){
        s2+=arr[i].toString(36)
    }
    return s2
}
de=function(s){
    var arr=s.split(/(..)/)
    var arr2=[]
    var s2=''
    for(var i in arr){
        if(arr[i]){
            arr2.push(parseInt(arr[i],36))
        }
    }
    for(var i in arr2){
        s2+=String.fromCharCode(arr2[i])
    }
    return s2.replace(/\+/g,' ')
}

While the encoded string is larger than I would have liked, it could be useful for protecting cookies from being hacked or something.

For example, using the example I made here, I made this message 21173b2x3030172t2p38173d333936172u2p2r2t as well as this ЎDŽ̺ǦDŽ̌. And if you manage to decode the second one, then decode this too ʒ̴ͻϮ

This isn’t very practical, though, so I’ll probably end up using the library Ragnarokkr linked me to.

Thank you all for the responses!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:38:10+00:00Added an answer on June 17, 2026 at 8:38 am

    Try this:

    function encode(string) {
        var number = "0x";
        var length = string.length;
        for (var i = 0; i < length; i++)
            number += string.charCodeAt(i).toString(16);
        return number;
    }
    

    See the demo here: http://jsfiddle.net/hGKAg/2/

    Decoding is just as simple:

    function decode(number) {
        var string = "";
        number = number.slice(2);
        var length = number.length;
        for (var i = 0; i < length;) {
            var code = number.slice(i, i += 2);
            string += String.fromCharCode(parseInt(code, 16));
        }
        return string;
    }
    

    Try the demo: http://jsfiddle.net/hGKAg/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so there's another similar question that I found to this on SO but
Please note: This question is very similar to this one I found on stackoverflow.
I'll preface this question with the note that I have looked at this similar
Possible Duplicate: Priority queue in .Net This question is similar, but i want to
On the php documentation,i found this note: On both 32 and 64-bit systems (OS
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
This seems to be a similar question ( Test DAO in java using Junit4
Note: I had a previous similar question which I've now attempted to delete. I
Note: I've included all my console output as pastebin links to keep this question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.