Skip to content

Instantly share code, notes, and snippets.

@joni
joni / toUTF8Array.js
Last active June 30, 2023 04:02
toUTF8Array: Javascript function for encoding a string in UTF8.
function toUTF8Array(str) {
var utf8 = [];
for (var i=0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
@joni
joni / maybe_utf8_decode.sql
Created August 2, 2012 11:20
maybe_utf8_decode: MySQL function to recover doubly UTF-8 encoded text
DELIMITER $$
CREATE FUNCTION maybe_utf8_decode(str text charset utf8)
RETURNS text CHARSET utf8 DETERMINISTIC
BEGIN
declare str_converted text charset utf8;
declare max_error_count int default @@max_error_count;
set @@max_error_count = 0;
set str_converted = convert(binary convert(str using latin1) using utf8);
set @@max_error_count = max_error_count;
@joni
joni / STRINGDECODE.sql
Created June 19, 2012 19:35
STRINGDECODE: MySQL function to decode unicode escapes to utf8
CREATE FUNCTION STRINGDECODE(str TEXT CHARSET utf8)
RETURNS text CHARSET utf8 DETERMINISTIC
BEGIN
declare pos int;
declare escape char(6) charset utf8;
declare unescape char(3) charset utf8;
set pos = locate('\\u', str);
while pos > 0 do
set escape = substring(str, pos, 6);
set unescape = char(conv(substring(escape,3),16,10) using ucs2);
set terminal pngcairo font "arial,8" size 640,350
set output 'simple_distance.png'
set multiplot layout 1, 2
r(d) = d*pi/180
dist_hav(x,y,x0,y0) = 2*asin(sqrt(sin(r(y-y0)/2)**2 + cos(r(y0))*cos(r(y))*sin(r(x-x0)/2)**2))*6371
dist_sim(x,y,x0,y0) = sqrt(r(y-y0)**2 + (cos(r(y0))*r(x-x0))**2)*6371
error(x,y,x0,y0) = 100*abs(dist_hav(x,y,x0,y0)-dist_sim(x,y,x0,y0))/dist_hav(x,y,x0,y0)
set view map
@joni
joni / float2rat.js
Last active November 29, 2018 15:11 — forked from anonymous/float2rat.js
A JavaScript function to find short rational number representations of floating point numbers, with the help of some continued fractions.
function float2rat(x) {
var tolerance = 1.0E-6;
var h1=1; var h2=0;
var k1=0; var k2=1;
var b = x;
do {
var a = Math.floor(b);
var aux = h1; h1 = a*h1+h2; h2 = aux;
aux = k1; k1 = a*k1+k2; k2 = aux;
b = 1/(b-a);
@joni
joni / Test.java
Created June 12, 2012 16:56
MySQL stored procedure with UTF-8 parameters and a JDBC client
import java.sql.*;
class Test {
public static void main(String [] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test?characterEncoding=utf8", "user", "pass");
Statement s = conn.createStatement();
s.execute("call hello('겠겠지만');");
ResultSet rs = s.getResultSet();
rs.next();
@joni
joni / gist:1695782
Created January 28, 2012 21:16
Resize and crop an image in PHP and GD
<?php
$srcPath = "your source image path goes here";
$dstPath = "your destination image path goes here";
$size = "90x90";
list($w, $h, $type) = getimagesize($srcPath);
switch ($type) {
case IMAGETYPE_JPEG:
$src = imagecreatefromjpeg($srcPath);