• If you need help or want to discuss things, you now can also join us on our Discord Server!

Solved Mysql varchar returned as array of ASCII chars?

Trsak

Member
Hello,
why is result of mysql query returning varchars not as string, but as array of ASCII chars?

JavaScript:
dbc.query("SELECT length as icon, type FROM `server_items_types` WHERE `id` = ? LIMIT 1", res[0].type, function (err3, res2, fields) {
                          if (err3) {
                            reply(err3);
                          } else {
                            if (res2 && res2.length && res2.length > 0) {
                              var type = JSON.stringify(res2[0].type)
                              var icon = helpers.toString(res2[0].icon)
                              engine.log(type)
                              engine.log(icon)
                          }
                        })

This Prints:
[105,99,111,110]
"53,49,54,52,51"
 

Multivitamin

Well-Known Member
Tier III
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
this are actually the number representation of each character, if you want to convert them with full unicode support use this:

JavaScript:
const helpers = require("helpers")

function parseString(numberBuffer) {
  if (!Array.isArray(numberBuffer)) return ""
  const bytewriter = helpers.newBytes()
  numberBuffer.forEach(num => bytewriter.append(helpers.bytesFromHex(num.toString(16))))
  return bytewriter.toString()
}

engine.log(parseString(type))
 

flyth

is reticulating splines
Staff member
Developer
Contributor
Or

JavaScript:
function parseString(val) {
    return Array.isArray(val) ? val.map(c => String.fromCharCode(c)).join('') : '';
}

That should be faster, actually.
 

Multivitamin

Well-Known Member
Tier III
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
But note that this wont work for unicode characters
 

Similar threads

Top