Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

[resolved] hex string to bytes

If you have a question or need help, this is the place to be.
Post Reply
User avatar
CommandFusion
Experienced User
Posts: 59
Joined: Thu Jun 25, 2009 9:36 am
Location: Melbourne, Australia
Contact:

[resolved] hex string to bytes

Post by CommandFusion »

I'm trying to send a string with hex bytes mixed in, denoted as \xFF format
The string value is created using a configuration dialog for an action.

The problem comes when the hex values defined within the string arent converted to their hex bytes, so:

Code: Select all

def __call__(self, Value):
        val = eg.ParseString(Value)
        print(val)
        self.plugin.Push(val)
that doesnt work, but defining a string literal does:

Code: Select all

def __call__(self, Value):
        print("l1=0c\x1Es50=test 1\x0D1c\x1Es50=test 2\x0D\x03")
        self.plugin.Push("l1=0c\x1Es50=test 1\x0D1c\x1Es50=test 2\x0D\x03")
So, how do I get python to magically convert the string within the variable (defined by a configuration dialog) to its hex bytes?

Thanks!
Last edited by CommandFusion on Wed Nov 18, 2009 9:47 pm, edited 1 time in total.
Jarrod Bell
CommandFusion
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: hex string to bytes

Post by Pako »

My English is very weak and I might not understand what you need.
Your post is almost incomprehensible to me. Nevertheless, I will be happy to help.
Convert string to hex representation can be as follows:

Code: Select all

import binascii
hexRepr=binascii.b2a_hex(string)
Or as follows:

Code: Select all

def String2hex(string):
    tmp=[]
    for char in string:
        tmp.append("\\x%02X" % ord(char))
    return ''.join(tmp)
hexRepr=String2hex(string)
Pako
User avatar
CommandFusion
Experienced User
Posts: 59
Joined: Thu Jun 25, 2009 9:36 am
Location: Melbourne, Australia
Contact:

Re: hex string to bytes

Post by CommandFusion »

This just converts it to a string with all data converted to \xFF format:

\x6C\x31\x3D\x30\x63\x5C\x78\x31\x45\x73\x35\x30\x3D\x74\x65\x73\x74\x20\x31\x5C\x78\x30\x44\x31\x63\x5C\x78\x31\x45\x73\x35\x30\x3D\x74\x65\x73\x74\x20\x32\x5C\x78\x30\x44\x5C\x78\x30\x33

It should print out like the selected line in the attached image, with just the non printable hex chars as squares:
Attachments
Picture 1.png
Jarrod Bell
CommandFusion
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: hex string to bytes

Post by jinxdone »

The problem is that at some point between the configuration dialog and the action of your plugin the string gets an extra layer of escape characters, like as if it would be input as a raw string. In other words any escape characters '\' in the original string convert into '\\'.

Now, what you should do is take off one layer of this string escaping with string.decode('string_escape').

Code: Select all

def __call__(self, Value):
        val = Value.decode('string_escape')
        val = eg.ParseString(val)
        print(val)
        self.plugin.Push(val)
-jinxdone
User avatar
CommandFusion
Experienced User
Posts: 59
Joined: Thu Jun 25, 2009 9:36 am
Location: Melbourne, Australia
Contact:

Re: hex string to bytes

Post by CommandFusion »

THANK YOU! works perfect.

I had tried decode("hex") but obviously wasn't what I was after ;)
Jarrod Bell
CommandFusion
Post Reply