1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Setting a string into a hexidecimal value

Discussion in 'Programming' started by Misu, Feb 18, 2017.

  1. Misu

    Misu The forum's immigrant

    Joined:
    Jun 20, 2016
    Posts:
    660
    I am trying to change a number value into its corresponding character hexidecimal value for use as my own custom file encryption. I am trying to figure out if there is a possibility to assign a string to a hexidecimal like so:

    "00F8" into $00F8

    These are NOT colours by the way. They are special characters for text.

    This is something experimental, I am not doing anything in specific. Just want to know the possibilities. Thanks in advance. :)
     
  2. PandaPenguin

    PandaPenguin Guest

    yourVariable = "$" + string(yourValue)

    yourValue can be anything from pure numbers to mixed letters/numbers

    EDIT: or I misread your question :p
     
    Last edited by a moderator: Feb 19, 2017
  3. Tthecreator

    Tthecreator Your Creator!

    Joined:
    Jun 20, 2016
    Posts:
    757
    Just convert it to a number?
    The character A starts at asci 65. the 0 at 48. Your number is thus:
    Code:
    char=//some for loop looping trough characters
    var asci = ord(char)
    if asci<65 then{
    number = asci-48
    }else{
    number = asci-65
    }
    
    Then you can add those numbers using multiplication:
    Code:
    finalnumber = number4 * (16*16*16) + number3 * (16*16) + number2 * (16) + number1
    
    I hope that helps you.
     
  4. Misu

    Misu The forum's immigrant

    Joined:
    Jun 20, 2016
    Posts:
    660
    @PandaPenguin I already tried that but it returns me in blank. If I do $00F8 normally, it would display it. However, when I did "$"+"00F8" is did not.

    @Tthecreator That only applies to ASCII. I am using UTC-8, which means I need to use special characters outside the 255 limit.
     
  5. PandaPenguin

    PandaPenguin Guest

    ok so "stitching" together a string doesn't work as it gets recognized as string and not hex value... I'm not sure that GML supports any more hexadecimal variable processing other than the color coding
     
  6. Tthecreator

    Tthecreator Your Creator!

    Joined:
    Jun 20, 2016
    Posts:
    757
    what happens if you just put the byte numbers back together like this:
    Code:
    string=""
    c1 = number2 * (16) + number1
    c2 = number4 * (16) + number3
    string=chr(c1)+chr(c2)
    
    I don't actually know if chr does something special with your data, but I hope it will just throw any number below 255 into string format, whatever it means. That means that utf-8 formatting should be preserved. (if my assumptions are right.)
     
  7. Misu

    Misu The forum's immigrant

    Joined:
    Jun 20, 2016
    Posts:
    660
    Nevermind. I found out that I had to use only chr() alone and it returns me what I needed. My bad. Thanks Guys!
     
  8. PandaPenguin

    PandaPenguin Guest

    ok, good to know :D
     
  9. Tthecreator

    Tthecreator Your Creator!

    Joined:
    Jun 20, 2016
    Posts:
    757
    good, but that will not actually work with utf-8 I just found out. Only with plain unicode characters, but that's not an actual format.
    The utf-8 format needs to be converted to unicode, which is some work on itself.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice