2012/07/13

避風港

真的有避風港,還在淡水...


檢視較大的地圖

2012/07/06

Get cpu cores number in linux

Output:
cpu count: 4

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main() {
  5.     int num = 1;
  6.     char key[512], deli[512], value[512];
  7.     FILE* fd = fopen("/proc/cpuinfo", "r");
  8.     if(fd != 0)
  9.     {
  10.         while(!feof(fd))
  11.         {
  12.             memset(key, 0, sizeof(key));
  13.             memset(value, 0, sizeof(value));
  14.             fscanf(fd, "%s%s%[^\n]", key, deli, value);
  15.             if(strcmp(key, "processor") == 0)
  16.             {
  17.                 sscanf(value, "%d", &num);
  18.                 num++; /* processor index is 0, 1.... should add 1 */
  19.             }
  20.         }
  21.         fclose(fd);
  22.     }
  23.     printf("cpu count: %d\n", num);
  24.     return 0;
  25. }

2012/06/20

smtp over starttls


Beware, "\r\n" means the return key right after you issue a command.

C:\>telnet smtp.gmail.com 587

220 mx.google.com ESMTP nh6sm29764930pbc.44

Then input

EHLO\r\n

250-mx.google.com at your service, [114.34.198.249]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

STARTTLS\r\n

220 2.0.0 Ready to start TLS

And then SSL connect should be established.

2012/04/19

Size of memory allocation(malloc) for c language

From: BASIC programming forum - "C": Size of memory allocated to a pointer?


#include <stdio.h> 
#include <stdlib.h> 
#include <alloc.h> 

void *my_alloc(int size) {
    int *block;

    /* bump up request so we have a place to store the size */
    block = malloc(size+sizeof(int));
    if (block == NULL)
        return NULL;

    /* store the size */
    *block = size;
    /* and return pointer to next place (automatically scaled) */
    return block + 1;
}

void my_free(void *block0) {
    int *block = block0;

    /* go back to the actual allocation address */
    block--;
    free(block);
}

int my_msize(void *block0) {
    int *block;

    /* back up to the size of the block */
    block = (int *)block0 - 1;

    return *block;
}

int main() {
    char *p;
    int rc;

    p = my_alloc(50);
    printf("Size of p is %d\n", my_msize(p));
    my_free(p);
    return 0;
}

2012/01/06

c# hex str transformation

Form looks like this...

Code is below...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace StrHex
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             EncodingInfo[] infos = Encoding.GetEncodings();
  22.             for (int i = 0; i < infos.Length; i++)
  23.                 comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
  24.  
  25.             if (comboBox1.Items.Count > 0)
  26.                 comboBox1.SelectedIndex = 0;
  27.         }
  28.  
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  32.             Encoding encode = Encoding.GetEncoding(name);
  33.             if (encode != null)
  34.             {
  35.                 Byte[] bs = encode.GetBytes(textBox1.Text);
  36.                 if (bs.Length * 2 > textBox1.MaxLength)
  37.                     return;
  38.  
  39.                 StringBuilder sb = new StringBuilder();
  40.                 foreach (Byte b in bs)
  41.                     sb.AppendFormat("{0:X2}", b);
  42.                 textBox1.Text = sb.ToString();
  43.             }
  44.         }
  45.  
  46.         private void button2_Click(object sender, EventArgs e)
  47.         {
  48.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  49.             Encoding encode = Encoding.GetEncoding(name);
  50.             if (encode != null)
  51.             {
  52.                 if (textBox1.TextLength % 2 == 0)
  53.                 {
  54.                     ArrayList al = new ArrayList();
  55.                     for (int i = 0; i < textBox1.TextLength; i += 2)
  56.                     {
  57.                         Byte b = 0;
  58.                         string hex = textBox1.Text.Substring(i, 2);
  59.                         if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
  60.                             al.Add(b);
  61.                         else
  62.                             return;
  63.                     }
  64.                     textBox1.Text = encode.GetString((Byte[])al.ToArray(typeof(Byte)));
  65.                 }
  66.             }
  67.         }
  68.  
  69.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  70.         {
  71.             if (e.Control && e.KeyCode == Keys.A)
  72.                 textBox1.SelectAll();
  73.         }
  74.  
  75.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  76.         {
  77.             if (e.Control && e.KeyCode == Keys.A)
  78.             {
  79.                 textBox1.SelectAll();
  80.                 textBox1.Focus();
  81.             }
  82.  
  83.             if (e.Control && e.KeyCode == Keys.H)
  84.                 button1.PerformClick();
  85.  
  86.             if (e.Control && e.KeyCode == Keys.S)
  87.                 button2.PerformClick();
  88.  
  89.             if (e.Control && e.KeyCode == Keys.W)
  90.                 this.Close();
  91.         }
  92.  
  93.         private void textBox1_TextChanged(object sender, EventArgs e)
  94.         {
  95.             label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
  96.         }
  97.     }
  98. }
  99.