Use binary COPY table FROM with psycopg2

Here is the binary equivalent of COPY FROM for Python 3: from io import BytesIO from struct import pack import psycopg2 # Two rows of data; “id” is not in the upstream data source # Columns: node, ts, val1, val2 data = [(23253, 342, -15.336734, 2494627.949375), (23256, 348, 43.23524, 2494827.949375)] conn = psycopg2.connect(“dbname=mydb user=postgres”) curs … Read more

How to Embed/Link binary data into a Windows module

Right click the resource script (.rc file) Choose Import http://msdn.microsoft.com/en-us/library/saced6x2.aspx You can embed any “custom” file you want, as well as things like .bmps and stuff VisualStudio “knows” how to edit. Then you can access them with your framework’s resource functions like FindResource LoadResource etc… If you don’t have a resource script. Click Project Add … Read more

SQL Query with binary data (PHP and MySQL)

Note: This addresses binary data, but not encrypted data. See this answer for searching on encrypted data. Try adding X, x or 0x in front of binary data used for search: SELECT id FROM test WHERE pid = ‘0xÞFÈ>ZPÎ×jRZ{æ×’; EDIT: try also this: SELECT id FROM test WHERE BINARY pid = ‘ÞFÈ>ZPÎ×jRZ{æ×’; OR SELECT id … Read more

How do I implement hex2bin()?

To answer your question: function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)} Here are some further functions you may find useful for working with binary data: //Useful Functions function checkBin(n){return/^[01]{1,64}$/.test(n)} function checkDec(n){return/^[0-9]{1,64}$/.test(n)} function checkHex(n){return/^[0-9A-Fa-f]{1,64}$/.test(n)} function pad(s,z){s=””+s;return s.length<z?pad(“0″+s,z):s} function unpad(s){s=””+s;return s.replace(/^0+/,”)} //Decimal operations function Dec2Bin(n){if(!checkDec(n)||n<0)return 0;return n.toString(2)} function Dec2Hex(n){if(!checkDec(n)||n<0)return 0;return n.toString(16)} //Binary Operations function Bin2Dec(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(10)} function Bin2Hex(n){if(!checkBin(n))return 0;return … Read more

Line reading chokes on 0x1A

0x1A is Ctrl-Z, and DOS historically used that as an end-of-file marker. For example, try using a command prompt, and “type”ing your file. It will only display the content up the Ctrl-Z. Python uses the Windows CRT function _wfopen, which implements the “Ctrl-Z is EOF” semantics.