1

I have a scenario where I have to lodge file path to DB and while fetching the content, I am getting the special characters converted into Hexadecimal values. But with that path, I would not be able to open the files, so I need them in a normal string. I tried searching a lot but could not get the solution,
for example, I am getting the output as

"D:\nhubh\x07oo\x08\x07m"

and my expectation is:

D:\nhubh\aoo\b\am

Please help. Thanks in advance :)

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 1
    Welcome to Stack Overflow. You may be dealing with a character set issue in your database. Do `SHOW CREATE TABLE whatever` and look for `CHARSET` clauses, both on your column and on the whole table. Also, look for character set settings in your Python environment. Then, [edit] your question to provide more information. Thanks! – O. Jones Jun 07 '18 at 10:33
  • https://stackoverflow.com/questions/9641440/convert-from-ascii-string-encoded-in-hex-to-plain-ascii Refer this link – Surya Tej Jun 07 '18 at 10:37
  • @SuryaTej, The problem is my string is not all Hexadecimal only some characters of it would be Hex, and the provided link solves the problem for whole Hexadecimal String. – Shubham Shrivastava Jun 07 '18 at 10:46
  • @O.Jones, I am very new to MySQL,do we have some datatype which can store the raw string and return as it is? I think the problem is where I am doin `cursor.fetchall()`, while fetching the data from SQL and storing them in a tuple. – Shubham Shrivastava Jun 07 '18 at 10:49
  • There's no such thing as a raw string. All strings have character encoding, be it ASCII, ISO-8859-1, or utf8. As a programmer, you must deal with this. Many programming environments use unicode and store their data encoded as utf8, so it's transparent. But the version of MySQL you use does not set unicode / utf8 as default. Plus, `\x07` and `\x08` translate to the control characters BELL and BACKSPACE. Try doing SELECT col, HEX(col) FROM table` from an ordinary mysql client (phpmyadmin, HeidiSQL, etc) to see what's actually stored in your column. – O. Jones Jun 07 '18 at 11:00

1 Answers1

0

Where was D:\\nhubh\aoo\b\am used?

If it is in a string being fed to, say INSERT, then the backslashes need to be escaped:

D:\\\\nhubh\\aoo\\b\\am

If you are trying use that as a file path, then either of these works:

D:\\\\nhubh\\aoo\\b\\am
D://nhubh/aoo/b/am

Partial explanation:

The backspace 'character' is hex 08 or represented in some contexts as \b. Similarly a=alert=07

Rick James
  • 135,179
  • 13
  • 127
  • 222