×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    decode hex using raw input?

    hia guys, another n00b question lol

    im using a simple way of decoding long hex strings at the moment:

    a = " hex code goes here ".decode("hex")
    print a

    but it means when i go to decode more hex i have delete all the hex iv entered into the script. I was wondering if there was a way to make this work using the raw_input command so i only have to run the script and enter the long hex strings to be decoded and save me the trouble of having to delete stuff from the script all the time.

    I tried using this :

    a = raw_input("Enter Hex Code :")
    b = ("%s") % a
    dec_hex = "b".decode("hex")
    print dec_hex

    But unfortunatly that doesnt work, can anyone point out where im going wrong here thanks

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: decode hex using raw input?

    Hi,

    I tried this example and it worked:

    Code:
    a=raw_input("Enter hex code:")
    #Entered 00611C
    a=a.decode("hex")
    a  #Returns \x00a\x1c
    Last edited by bogdan.galiceanu; 2008-04-02 at 18:50.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Re: decode hex using raw input?

    Hia thanks for your reply bogdan, I tried the example you showed me but it only returned the 00611C that im entering? any idea why it wont work for me?

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: decode hex using raw input?

    Quote Originally Posted by gormmark View Post
    Hia thanks for your reply bogdan, I tried the example you showed me but it only returned the 00611C that im entering? any idea why it wont work for me?
    Sorry, I forgot to assign the decoded value to a. I've edited my post and it should work now.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Re: decode hex using raw input?

    Hia i tried the new code you edited but this time it only returns the a thats been assigned to it

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Re: decode hex using raw input?

    Quote Originally Posted by gormmark View Post
    Hia i tried the new code you edited but this time it only returns the a thats been assigned to it
    its ok bogdan it was a user error by me lol its working now thanks very much for your help mate. much appreciared.

  7. #7
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: decode hex using raw input?

    You're very welcome, glad I was able to help.

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    115

    Re: decode hex using raw input?

    Quote Originally Posted by bogdan.galiceanu View Post
    Hi,

    I tried this example and it worked:

    Code:
    a=raw_input("Enter hex code:")
    #Entered 00611C
    a=a.decode("hex")
    a  #Returns \x00a\x1c
    Sorry to bring this up but I've tried this and it comes up with an error if I try and enter the value of 00611C I'm trying to convert HEX to an int or string.

  9. #9
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: decode hex using raw input?

    Quote Originally Posted by Dansco View Post
    I'm trying to convert HEX to an int or string.
    Here's a function for converting hex to int. I did a few tests with an online converter and the values returned by this function seem to be correct. Hope it helps:
    Code:
    def hex_to_int(s):
           return int(s, 16)
    As for the conversion to string, this might work:
    Code:
    def hex_to_str(s):
           a=""
           for i in range(len(s)/2):
                   r=i*2
                   a+=chr(int(s[r:r+2],16))
           return a
    Last edited by bogdan.galiceanu; 2008-06-10 at 06:03. Reason: typo

  10. #10
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    115

    Re: decode hex using raw input?

    Quote Originally Posted by bogdan.galiceanu View Post
    Here's a function for converting hex to int. I did a few tests with an online converter and the values returned by this function seem to be correct. Hope it helps:
    Code:
    def hex_to_int(s):
           return int(s, 16)
    As for the conversion to string, this might work:
    Code:
    def hex_to_str(s):
           a=""
           for i in range(len(s)/2):
                   r=i*2
                   a+=chr(int(s[r:r+2],16))
           return a
    Cheers mate. I found out how to do it by total accident but thanx for the help

Similar Threads

  1. UIQ2.1 MultiViews & CEikEdwin handwrite input troubles?
    By goready@163.com in forum Symbian
    Replies: 0
    Last Post: 2005-10-16, 09:19
  2. numeric input on nokia6820
    By marcelk00l in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2004-07-16, 14:43
  3. subscribing to Analog Input changes
    By fernanfr in forum Nokia M2M
    Replies: 1
    Last Post: 2003-03-04, 10:32
  4. SMS from change in N30 input pin
    By vhkauppi in forum Nokia M2M
    Replies: 2
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×