var tyu=""; var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'franshyza' }); connection.connect(); connection.query('SELECT * FROM `ee`', function (error, results, fields) { // a1 из mysql в кодировке utf8!!! // a1='\xff\xff\xff\xff\x66\x0a\x4d\xdc\xb4\xb4\x69\x87\x00\x00\x00\x00\x00\x00' // Но длинна a1=72 // А a2=18 (как надо) // ПОЧЕМУ И КАК ИСПРАВИТЬ??(( a1=results[0].ip; }); connection.end(); var socket = require( "dgram" ).createSocket( "udp4" ); socket.on( "message", function ( message, requestInfo ) { ); //ПРОБЛЕМАаааааааааааааааааааа for (var i = 0; i < 1; i++) { var a2='\xff\xff\xff\xff\x66\x0a\x4d\xdc\xb4\xb4\x69\x87\x00\x00\x00\x00\x00\x00'; console.log(a1.length ); console.log(a2.length ) ; var response = new Buffer(a2, 'ascii'); 
  • funny, unicode codes were not recognized, if in a2 you screen slashes the length of both will be 72 - Grundy
  • @Grundy, I didn't understand anything at all ... - Qwertiy
  • @Qwertiy, \xff character code, a string comes from the base with 4 characters instead of one: '\', 'x', 'f', 'f' - that's the difference :) - Grundy
  • one
    Most likely the problem is how this data is stored - Grundy
  • how do i make a1 as well equal to 18? in mysql the data in utf8 (if that matters) - user210260

1 answer 1

You receive hexadecimal codes instead of characters. You need to convert them to normal characters using String.fromCharCode(parseInt(char,16))

 function charConvert(str){ var a1 = []; a2.split("\\x").slice(1,a2.length).forEach(function(item, i, arr) { a1[i] = String.fromCharCode(parseInt(item,16)); }) return a1.join(""); } 

Well, or as a normal option, long and tedious to deal with encodings. Try there to convert to another encoding and give the line in the initially normal form or send some SQL to change the encoding to the desired one or change the encoding of the file. In general, there is a need to look.

  • Thank you so much! Everything is working! - user210260