string to Hexadecimal

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

string to Hexadecimal

Post by pink » Sat Jan 30, 2016 9:09 pm

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.
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: string to Hexadecimal

Post by FourthWorld » Sat Jan 30, 2016 9:13 pm

I think the binaryDecode function may be what you're looking for.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

Re: string to Hexadecimal

Post by pink » Sat Jan 30, 2016 11:03 pm

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
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: string to Hexadecimal

Post by FourthWorld » Sat Jan 30, 2016 11:18 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

Re: string to Hexadecimal

Post by pink » Mon Feb 01, 2016 9:42 pm

Thanks. "H*" (capital) was what I needed. So far so good.

binaryDecode("H*",theString,theHex)
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

Post Reply