To find out if a file exists or not, use:
if(System.IO.File.Exists(path)) { ... }
for a folder, use:
if(System.IO.Directory.Exists(path)) { ... }
To find out if a file exists or not, use:
if(System.IO.File.Exists(path)) { ... }
for a folder, use:
if(System.IO.Directory.Exists(path)) { ... }
Encoding string to Base64 format:
public String Encode2Base64(String text) { try { return Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(text)); } catch (Exception) { return null; } }
Decoding string from Base64 format:
public String DecodefromBase64(String text) { try { return Encoding.ASCII.GetString(Convert.FromBase64String(text)); } catch (Exception) { return null; } }
A good reason to use prepared statements is to avoid SQL injection without needing to escape data.
Connect
$conn = mysqli_connect("localhost", "user_name", "xxxxxxxx", "dbname"); if ($conn->connect_errno) { echo "Connection failed: " . $conn->connect_errno . " - " . $conn->connect_error; }
New statement
$stmt = $conn->stmt_init();
Execute Prepared Statement
if($stmt->prepare("INSERT INTO `users` (`Username`, `Password`, `Salt`) VALUES (?, ?, ?)")) { $stmt->bind_param('sss', $username, $passwd, $salt); //sss -> string string string $username = 'JohnDoe'; $passwd = '3g435g245g45g54h3455h544h2'; $salt = 'h45534h634GWgewgESgwEgseagGSAEGg4634h634h'; $stmt->execute(); $stmt->close(); } else{ echo "Prepare failed: " . $conn->errno . " " . $conn->error; }
Data types – bind_param
i – integer
d – double
s – string
b – blob – binary
Disconnect
$conn->close();
Default regions for Drupal 7
regions[header] = Header regions[highlighted] = Highlighted regions[help] = Help regions[content] = Content regions[sidebar_first] = Left sidebar regions[sidebar_second] = Right sidebar regions[footer] = Footer
You can override the values for your specific needs by editing your theme’s info file.
If you have broken everything by renaming, editing or removing the default theme and you have trouble signing in to administration panel then in this case you can fix the problem by changing Drupal’s default theme in MySQL database. Open PHPMyAdmin or MySQL Workbench and execute the following sql queries.
Set Garland theme as default:
UPDATE system SET STATUS=1 WHERE name = 'garland';
Change the variable theme_default value:
UPDATE variable SET VALUE='s:7:"garland"' WHERE name = 'theme_default';
Clear cache:
TRUNCATE cache;
1. Download json-simple library (.jar file) from here.
2. Add a reference to the json-simple jar file.
3. Import package org.json.simple.JSONObject
4. Write your code:
JSONObject jsonobj = new JSONObject(); jsonobj.put("city", "Athens"); jsonobj.put("country", "Greece"); System.out.println(jsonobj.toString());
Let’s see how we can convert text into an image using C#:
1 2 3 4 5 6 7 8 9 10 11 12 | public Image DrawText(String text) { Image img = new Bitmap(64, 64); Graphics drawing = Graphics.FromImage(img); drawing.Clear(Color.Yellow); Brush textBrush = new SolidBrush(Color.Black); drawing.DrawString(text, new Font(new FontFamily("Trebuchet MS"), 12f), textBrush, 0, 0); drawing.Save(); textBrush.Dispose(); drawing.Dispose(); return img; } |
Magic numbers are the first bits of a file which uniquely identify the type of the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class FileIdentifier { private final int BMP = 0x424d; private final int GIF = 0x47494638; private final int JPEG = 0xffd8ffe0; private final int PNG = 0x89504e47; /** * get image file type based on file first bytes - magic number * @param filename * @return format name */ public String getImageFileType(String filename) { try { try (DataInputStream input = new DataInputStream( new BufferedInputStream( new FileInputStream(filename)))) { for (int i = 0; i < 1; i++){ switch(input.readInt()){ case BMP: return "bitmap"; case GIF: return "gif"; case JPEG: return "jpeg"; case PNG: return "png"; default: return "Unknown format"; } } return "Unknown format"; } } catch (IOException ex) { System.err.println(ex.getMessage()); return "Unknown format"; } } } |
First we are going to detect the mimetype from bytes array and then save the bytes into an image file.
/** * Save bytes array to image file * @param chunk */ public void Bytes2Image( byte[] chunk, String filename, String folder) { if(folder.charAt(folder.length()-1) != '/') folder += "/"; BufferedImage image; String formatName = "jpg"; try { ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(chunk)); Iterator<ImageReader> iter=ImageIO.getImageReaders(iis); if (iter.hasNext()) { ImageReader reader = (ImageReader)iter.next(); reader.setInput(iis); formatName = reader.getFormatName(); } image = ImageIO.read( new ByteArrayInputStream( chunk ) ); ImageIO.write(image, formatName, new File(folder+filename+"."+formatName.toLowerCase())); } catch (IOException ex) { System.err.println(ex.getMessage()); } }
Example
Bytes2Image(myimagebytes, "my_image_file", "/path/to/images");
Library
To create QR codes we are going to use open source library libqrencode.
Installation
I you are in Ubuntu install libqrencode by issuing the following command:
sudo apt-get install libqrencode libqrencode-dev
Source code
#include <stdio.h> #include <qrencode.h> #include <errno.h> int main(){ QRcode *qrcode; qrcode = QRcode_encodeString("http://webnetsoft.gr", 4, QR_ECLEVEL_H, QR_MODE_8, 0); if(qrcode == NULL) { printf("An error has occured (%d).\n", errno); if(errno == EINVAL){ printf("Invalid input object\n"); } else if(errno == ENOMEM) { printf("Unable to allocate memory for input objects.\n"); } else if(errno == ERANGE) { printf("Input data is too large.\n"); } return 1; } printf("%s", qrcode->data); QRcode_free(qrcode); return 0; }
Compile
gcc -o encode encode.c -lqrencode