Page 1 of 1

string to Hexadecimal

Posted: Sat Jan 30, 2016 9:09 pm
by pink
I need to be able to convert a short string into its hexadecimal
eg converting "pink" to "70696e6b"

now, I've been using the following, but it runs into problems when I have non-English characters like letters with accent marks

Code: Select all

function stringToHex pUsername
   repeat for each char X in pUsername
      put charToNum(X) into tDecNum
      put baseConvert(tDecNum,10,16) after tHexNum
   end repeat
   return tHexNum
end stringToHex 
any suggestions on how to improve?

background: I am using a module in CouchDB that automatically creates and configures a new database for every new user that signs up. The username created is converted to Hex, and then the database is named "user-" and the hex result. So in order to access the database, when the user logins, I need to be able to accurately create the database name.

Re: string to Hexadecimal

Posted: Sat Jan 30, 2016 9:13 pm
by FourthWorld
I think the binaryDecode function may be what you're looking for.

Re: string to Hexadecimal

Posted: Sat Jan 30, 2016 11:03 pm
by pink
I tried using binaryDecode but couldn't get a response that worked

for example:
binaryDecode("h","b",theHex) returns "1"

I'm sure I'm missing something in this somewhere, but I keep trying different options

Re: string to Hexadecimal

Posted: Sat Jan 30, 2016 11:18 pm
by FourthWorld
The return value of the function is the number of converted types. In your example "theHex" will contain the converted value.

Also, "h" by itself will convert only one byte; try "h*" to convert all bytes in the second argument.

Re: string to Hexadecimal

Posted: Mon Feb 01, 2016 9:42 pm
by pink
Thanks. "H*" (capital) was what I needed. So far so good.

binaryDecode("H*",theString,theHex)