-3

The function below first decodes the hexadecimal number into a string and then the map function applies the ord function to all the decoded values and give us a tuple but this decode function is giving this error 'str' object has no attribute 'decode':

def hex2rgb(hexcode):
    return tuple(map(ord, hexcode[1:].decode( )))

The code that I am writing is pertain to steganography here some part of the code which shows how the input to that function is generated:

binary = str2bin(message) + '1111111111111110' #converts string into binary and adds the delimeter at end to indicate message is ended

newData = [] #list to store the new pixels which contain the information

img = Image.open(filename)

if img.mode in ('RGBA'):
    img = img.convert('RGBA') #will convert the image into RGBA
    datas = img.getdata()  #will give all the pixel data
    digit = 0 #shows on which binary bit we are currently at      
    for item in datas:
        if (digit < len(binary)):
            newpix = encode(rgb2hex(item[0],item[1],item[2]),binary[digit])
            if newpix == None:
                newData.append(item)
            else:
                r, g, b = hex2rgb(newpix)
                newData.append((r,g,b,255))
                digit += 1

I don't understand why it is not decoding. I also tried .decode('hex') or .decode(hex) but it is still giving the error.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
hunter
  • 11
  • 5
  • 1
    You can encode string and decode bytes. It seems like you might be trying to decode a string instead of byte, which is why you are get the error. See https://stackoverflow.com/questions/26125141 – zedfoxus Feb 17 '21 at 03:25
  • What is your input for hexcode and what do you expect for your output? – zedfoxus Feb 17 '21 at 03:32
  • Please provide a [mre] – Sabito stands with Ukraine Feb 17 '21 at 03:32
  • @ zedfoxus my input is a hexadecimal which is generated in the middle of code – hunter Feb 17 '21 at 04:05
  • @ zedfoxus no no i am trying to decode bytes in hexadecimal – hunter Feb 17 '21 at 04:05
  • @Yatin my code is long in the middle i am generating a hexadecimal which i am converting passing to this function and getting the tuple back – hunter Feb 17 '21 at 04:09
  • We don't need the whole code. Just a [mre]. For example, how you're calling that function. with what input and what is the expected output – Tomerikoo Feb 18 '21 at 09:07
  • @ Tomerikoo i have edited the question again and also added the minimal code can you again please have a look – hunter Feb 19 '21 at 02:51
  • @zedfoxus i have added the details – hunter Feb 19 '21 at 02:53
  • Above your `def` add `import base64`. Right after your `def` add `print(base64.b64encode(hexcode.encode('utf-8')))`. What output do you get? – zedfoxus Feb 19 '21 at 03:41
  • @zedfoxus its giving the same error – hunter Feb 20 '21 at 02:58
  • @zedfoxus i looked into google and found this method **codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()** well its for base64 so i changed "base64" into "utf-8" and made it **r, g, b = tuple(map(ord,codecs.encode(codecs.decode(hex, 'hex'), 'utf-8').decode())** now whatever written in next line it gives it a syntax error – hunter Feb 20 '21 at 06:10
  • @hunter Did you add the `print` statement after `def`? You will continue to get an error but before the error, a base64 string would have printed. Can you add the base64 as a comment? – zedfoxus Feb 21 '21 at 12:41
  • @hunter Can you update your question and include more information? What is the content of `message` variable? What is the content of `binary` variable? What does your `rgb2hex` function look like? Just above `r, g, b = hex2rgb(newpix)` line, do `print(base64.b64encode(newpix.encode('utf-8')))`. What does that print? – zedfoxus Feb 21 '21 at 13:05

1 Answers1

1

Your approach is incorrect. because you are trying to decode string that is already decoded. That's why the error is coming.

Mandalorian
  • 77
  • 1
  • 10
  • 1
    no its not decoded this function is written to decode the string. the input to function are bytes of hexadecimal – hunter Feb 17 '21 at 04:07
  • 1
    In my opinion it is more friendly and encouraging if you say something like "Your approach is incorrect..." rather than telling the person they are wrong. – devdanke Feb 17 '21 at 04:47
  • 1
    @devdanke i apologize i didnt meant to be disrespectful towards you. – hunter Feb 17 '21 at 11:29