真的有避風港,還在淡水...
檢視較大的地圖
每天都要寫程式
2012/07/06
Get cpu cores number in linux
Output:
cpu count: 4
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main() {
- int num = 1;
- char key[512], deli[512], value[512];
- FILE* fd = fopen("/proc/cpuinfo", "r");
- if(fd != 0)
- {
- while(!feof(fd))
- {
- memset(key, 0, sizeof(key));
- memset(value, 0, sizeof(value));
- fscanf(fd, "%s%s%[^\n]", key, deli, value);
- if(strcmp(key, "processor") == 0)
- {
- sscanf(value, "%d", &num);
- num++; /* processor index is 0, 1.... should add 1 */
- }
- }
- fclose(fd);
- }
- return 0;
- }
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;
}
#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...
Code is below...
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace StrHex
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- EncodingInfo[] infos = Encoding.GetEncodings();
- for (int i = 0; i < infos.Length; i++)
- comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
- if (comboBox1.Items.Count > 0)
- comboBox1.SelectedIndex = 0;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Encoding encode = Encoding.GetEncoding(name);
- if (encode != null)
- {
- Byte[] bs = encode.GetBytes(textBox1.Text);
- if (bs.Length * 2 > textBox1.MaxLength)
- return;
- foreach (Byte b in bs)
- sb.AppendFormat("{0:X2}", b);
- textBox1.Text = sb.ToString();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Encoding encode = Encoding.GetEncoding(name);
- if (encode != null)
- {
- if (textBox1.TextLength % 2 == 0)
- {
- for (int i = 0; i < textBox1.TextLength; i += 2)
- {
- Byte b = 0;
- string hex = textBox1.Text.Substring(i, 2);
- if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
- al.Add(b);
- else
- return;
- }
- }
- }
- }
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.A)
- textBox1.SelectAll();
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.A)
- {
- textBox1.SelectAll();
- textBox1.Focus();
- }
- if (e.Control && e.KeyCode == Keys.H)
- button1.PerformClick();
- if (e.Control && e.KeyCode == Keys.S)
- button2.PerformClick();
- if (e.Control && e.KeyCode == Keys.W)
- this.Close();
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
- }
- }
- }
訂閱:
意見 (Atom)