อยากจะบันทึกอะไรๆ ไปเรื่อยๆ ดีบ้างเลวบ้างสุดแท้แต่ มีทั้งเรื่อง การเมือง ไอที ความเชื่อ และอื่นๆ จะดีบ้าง อะไรบ้างก็แล้วแต่
29 พฤศจิกายน 2558
ว่าด้วยเรื่อง Unicode
เพราะต้องการจะเขียนให้จบเร็วจึงตัดสินใจใช้วิธีแปลงข้อมูลใน Table ของ MS Access เป็น SQL command (INSERT INTO) ลง Text File เพื่อเรียกใช้โดย sqlite3.exe command line จากโครงการ SQLite อีกทีหนึ่ง
ปัญหาคือแม้บอกว่าเป็น Unicode แต่ Unicode ก็ไม่ได้มีแบบเดียว มี UTF-8 และ UTF-16 แล้ว UTF-16 ยังมีแบบ Little Endian และ Big Endian อีก อย่างไรก็ตามโปรแกรมที่ทำการแปลงข้อมูลต้องทำบน Windows เพื่อให้คนอื่นใช้ บน Windows x86 เก็บ Character บน Memory เป็น 16 bit Little Endian
ทดลองเขียนไฟล์เป็น UTF16LE ก็ปรากฏว่า sqlite3.exe อ่านได้ แต่เก็บข้อมูลภาษาไทยผิด ทั้งที่จากการตรวจสอบไฟล์ SQL statement เปิดด้วย Text Editor ก็อ่านถูก เลยตัดสินใจเปลี่ยนเป็น UTF-8 ซึ่งต้องเปลี่ยน Library ด้วย เขียนแล้วก็เปิดด้วย Text Editor แสดงเป็น UTF-8 ได้ แต่ sqlite3.exe อ่านไม่ผ่าน เหมือนว่าไม่รู้จัก 3 byte แรก อะไรหว่า
ก็พบว่า UTF-8 Text File ก็ยังมี 2 แบบอีกคือ แบบมี BOM (Byte Order Mark) กับไม่มี ซึ่ง sqlite3.exe ต้องไม่มี เลยต้องตัด 3 byte แรกออกและเขียนเป็น Binary ลง Text File (ห้ามงง) ถึงจะผ่านฉลุย
อยากรู้ว่าทำไมชีวิตกับ Unicode ถึงอยู่ยากจัง
10 กุมภาพันธ์ 2558
ก็เขียนโปรแกรมตลอดมา และจะเขียนโปรแกรมตลอดไป
เมื่อยี่สิบกว่าปีก่อนตอนเป็นพนักงานขายซึ่งภายหลังเปลี่ยนมาเป็นพนักงานสนับสนุนการตลาดของบริษัทแห่งหนึ่ง ได้รู้จักพี่คนหนึ่งซึ่งเป็นผู้บริหารระดับ VP ของบริษัท ซึ่งงานที่ทำตามหน้าที่รับผิดชอบของเขาไม่น่าสนใจเท่าไรหรอก แต่ผมสนใจสิ่งที่พี่เขาทำตอนนั้นมากกว่า วันๆ เอาแต่นั่ง Download File มาทำการ Compile สิ่งเล็กๆ ที่เรียกว่า Linux บนเครื่อง 386 ด้วย Modem สุดหรู 9600 bps ยี่ห้อ Nokia และเริ่มได้ยินคำว่า Internet
จากที่เคยคิดกับตัวเองว่าเรียนจบจะไม่เขียนโปรแกรมอีก ก็เริ่มคิดใหม่ ตอนช่วงนั้นก็เลยกลับมาเขียนภาษา C พร้อมกับ VBA บน MS Access 2.0 ทั้งไปรับงานเขียนโปรแกรมที่โรงหนังแห่งหนึ่งมาทำ จากนั้นก็แยกย้ายกันไป ต่อมาพี่คนนั้นก็ไปเป็นถึง President บริษัท ISP แห่งหนึ่ง ก่อนจะออกไปและได้ข่าวว่าก็ยังเขียนโปรแกรมอยู่
ส่วนผมก็ทำงานจับฉ่ายไปทั่ว ไปขาย Router เขียน Web Application บน Windows และ Red Hat ขายของ ขายซอฟต์แวร์ กระโดดมาจับ Embedded System ออกแบบ Hardware และเขียน Firmware พอเจอระบบซับซ้อนก็เลยต้องมาใช้สิ่งเล็กๆ ที่เรียกว่า Linux ต้อง Compile Kernel และ Build Image เอง ดีกว่าพี่เขาหน่อยตรงที่เป็น 512 kbps แล้ว เขียน shell script เขียน C เปิด Socket ทำ Service ไปตามเรื่องตามราว มีบางโครงการต้องเขียน UDP/IP Stack เองบน ARM (Atmel SAM7X)
จากนั้นมีปัญหาสายตาก็ตัดสินใจว่าจะหยุดเขียนโปรแกรม ไปขายของ ไปทำ Information Security จนมาถึงจุดที่ต้องทำงานเกี่ยวกับ Log เครื่องมือในท้องตลาดมันไม่ได้ดั่งใจ เลยต้องมานั่งเขียน Filter เอง ทำ Data Analytics กับชุดข้อมูลที่ใหญ่เกินกว่า Excel จะรับไหว คือเกิน ล้านสองแสน Row ยิ่งต้องมาแตะ Application Security จากที่เคยคิดว่าจะไม่เขียนโปรแกรมอีก ก็กลับมาเขียนจนได้ ไม่งั้นโปรแกรมเมอร์ก็ไม่ยอมเชื่อ
ไม่รู้ว่าข้างหน้าจะเป็นยังไงแต่น่าจะได้ข้อสรุปล่ะว่าก็เขียนโปรแกรมตลอดมา และจะเขียนโปรแกรมตลอดไป
08 มิถุนายน 2553
Hungarian Notation
ตอนที่ทำงานเป็น Account Manager ดูแลการขายกับ Broker ได้เข้าไปดูกระบวนการเขียนซอฟต์แวร์ซื้อขายหลักทรัพย์แล้วก็สะดุดใจกับการ ตั้งชื่อตัวแปร เช่น szStockCode ก็นึกอยู่ว่าทำไมต้องมี sz นำหน้า ถามโปรแกรมเมอร์ก็ตอบว่าทาง Chicago Stock Exchange เจ้าของโปรแกรมบังคับให้ใช้ sz คือ null-terminate string ก็จำมาเรื่อย
จนกระทั่งเริ่มกลับมาเขียนโปรแกรมเองด้วย Visual Basic ก็เริ่มมีปัญหาในการจำว่าตัวแปรตัวไหนเป็นแบบไหน ก็เลยเริ่มค้นใน Microsoft Developer Network Library (MSDN Library) ก็พบเรื่อง VB Naming Convention ซึ่งประยุกต์มาจาก Hungarian Notation ที่ใช้กันในภาษา C
ไม่ได้คิดจะลงเรื่องวิชาการแต่อย่างใดเพียงแค่ลำดับความคิดเชื่อมโยงเท่านั้น
16 มิถุนายน 2550
Zip และ Unzip ด้วย PHP
บันทึกวิธีการ Zip/Unzip ด้วย PHP:
1. Download dUnzip2 จาก phpclasses.org มี 2 classes, dZip & dUnzip
require_once("dzip.inc.php");
$zip = new dZip("archive.zip");
$zip->addFile("myfile.doc", "myfile.doc"); // parameter แรกเป็น source file, parameter สองเป็น ชื่อ file ใน zip
$zip->addDir("subdir"); // สร้าง Folder ใน Zip
$zip->addFile("anotherfile.jpg", "subdir/newname.jpg"); // เพิ่ม file ใต้ Folder
$zip->save();
3. Unzip โดยใช้ dUnzip classrequire_once("dzip.inc.php");
$zip = new dUnzip("archive.zip");
$zip->unzipAll("targetfolder"); // Unzip ทุก file ไปยัง folder ชื่อ targetfolder
$zip->unzip("source.doc", "destination.doc"); // unzip source.doc ใน zip file เป็น destination.doc
28 พฤษภาคม 2550
php: baaGrid::setGPCol
ความสามารถหนึ่งที่ทำให้ baaGrid แสดงรายงานในแบบตารางได้ดีคือ General Purpose Column ซึ่งจะสามารถนำข้อมูลจาก query มาแสดงในรูปแบบอื่นได้เช่น image หรือ hyperlink แต่อย่างหนึ่งซึ่งตัว baaGrid Guide ไม่ได้บอกไว้ก็คือการสนับสนุนการใช้ function ได้ (**อย่างมีข้อจำกัด**) ทั้ง php built-in และ user defined โดยในการ parse funtion นั้น baaGrid ใช้ function_exists ของ php ตรวจอักขระที่ ตำแหน่ง 0 จนถึง วงเล็บเปิดแรก ถ้ามี function ที่รู้จักก็จะผ่านไปให้ eval ของ php ทำงาน
ตัวอย่าง
...
$grid->setGPCol(4, 'myfunction(#1)'); // pass column 1 from query to myfunction, valid for numeric data
$grid->setGPCol(5, 'myfunction("#2")'); // pass column 2 from query to myfunction, valid for string data
$grid->setGPCol(6, 'myfunction(#1)+123'); // also valid
...
$grid->setGPCol(6, '123+myfunction(#1)'); // invalid, function_exists('123+myfunction') returns null
11 พฤษภาคม 2550
wxWidgets ใช้ Image File แบบอื่นนอกเหนือ BMP
ถ้าจะใช้ Image File แบบอื่นนอกเหนือ BMP ต้องทำการระบุชนิดที่ต้องการใช้เพิ่มด้วยดังนี้
1 ใน class ที่ derived จาก wxApp ที่ OnInit จะต้องทำการ add image handler
#include
bool MyApp::OnInit() {
...
wxImage::AddHandler( new wxPNGHandler );
wxImage::AddHandler( new wxJPEGHandler );
wxImage::AddHandler( new wxGIFHandler );
wxImage::AddHandler( new wxXPMHandler );
}
ถ้าเอาทุกแบบใช้ wxInitAllImageHandlers.
Available Image Handlers
- wxBMPHandler For loading and saving Windows bitmap files.
- wxPNGHandler For loading and saving PNG files. Images with transparency or an alpha channel are supported.
- wxJPEGHandler For loading and saving JPEG files.
- wxGIFHandler GIF files: only for loading, due to legal issues.
- wxPCXHandler For loading and saving PCX files. wxPCXHandler will count the number of different colors in the image; if there are 256 or fewer colors, it will save as 8-bit; otherwise it will save as 24-bit.
- wxPNMHandler For loading and saving PNM files. Loading PNMs only works for ASCII or raw RGB images. When saving in PNM format, wxPNMHandler will always save as raw RGB.
- wxTIFFHandler For loading and saving TIFF files.
- wxIFFHandler For loading IFF files.
- wxXPMHandler For loading and saving XPM files.
- wxICOHandler For loading and saving Windows icon files.
- wxCURHandler For loading and saving Windows cursor files.
- wxANIHandler For loading Windows animated cursor files.
2. อย่าลืม Link library ที่เกี่ยวข้อง
- JPEG, wxjpeg.lib
- PNG, wxpng.lib & wxzlib.lib

