Monthly Archives: October 2011

Convert between byte sequence with bitconverter (c#)

BitConverter in C# provides a convenient way to: convert int to byte array or assemble int from a byte sequence. Of course, you can write your own routine to do all the work, but it’s good to know .net already have this built in. Please check out my test routine for an example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int testValue = 8;
            byte[] buffer = BitConverter.GetBytes(testValue);
            int convertBack = BitConverter.ToInt32(buffer, 0);

            Console.WriteLine("Test value : {0}", testValue);
            Console.WriteLine("Convert to byte array: {0}", BitConverter.ToString(buffer));
            Console.WriteLine("Convert to int from byte array: {0}", convertBack);
        }
    }
}

bit converter
Besides int, this could apply to all the basic built in types: float, double, int64. Also one nice thing, it could print out an byte array easily like this:
BitConverter.ToString(buffer)
Also, .net has built in the routines to convert between host and network byte order (big endian):
IpAddress.NetworkToHostOrder
it supports: int16, int32, int64. You can build your own routine to do this, but if you know this, would definitely save your from some work.

wordpress 3.2.1 – code tag lost after switch back from html view

wordpress editor is the best blog writer I have ever seen, but there is a flaw:
<code> tag would lost if you switch from html view back to visual view, for example, if you have code like this in html view:

<code>using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int testValue = 8;
            byte[] buffer = BitConverter.GetBytes(testValue);
            int convertBack = BitConverter.ToInt32(buffer, 0);

            Console.WriteLine("Test value : {0}", testValue);
            Console.WriteLine("Convert to byte array: {0}", BitConverter.ToString(buffer));
            Console.WriteLine("Convert to int from byte array: {0}", convertBack);
        }
    }
}</code>

And then you switch to Visual view, it messed up:

<code>using System;
using System.Collections.Generic;
using System.Text;</code>

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int testValue = 8;
byte[] buffer = BitConverter.GetBytes(testValue);
int convertBack = BitConverter.ToInt32(buffer, 0);

Console.WriteLine("Test value : {0}", testValue);
Console.WriteLine("Convert to byte array: {0}", BitConverter.ToString(buffer));
Console.WriteLine("Convert to int from byte array: {0}", convertBack);
}
}
}

Code tag is break before an empty line: line 3, and the extra white space is stripped. Wish it could be fixed in the future version.

Useful firefox shortcut key – firefox cheat sheet

Here are a few useful shortcut keys in firefox, once you’ve started, you will never get rid of:

  • Open a new tab: CTRL+T
  • close a tab: CTRL+W
  • move to the next tab: CTRL+TAB
  • restore previously closed tab: CTRL+SHIFT+T
  • go to search box: CTRL+E for firefox 3.6 or before, CTRL+K for firefox 4 or later.
  • open a link in new tab: CTRL+CLICK

Among them, this one is especially sweet, save you from an error action:
restore previously closed tab: CTRL+SHIFT+T

If you know some other cool keys missing here, feel free to leave a comment.

Useful pptpd server – poptop admin command

Here are a few commands useful for pptpd server admin:

Find out who is Online:

last | grep ppp | grep still

Get a list of user’s connection time:

ac -d -p
If you don’t have ac on your server, run:
apt-get install acct

Disconnect a User

  1. find out user’s ip (we know it from “Find out who is Online” part)
  2. use command below to find out process id
    ps -ef | grep | grep pppd
  3. end user’s process
    killĀ <process id>

Further Reading:

pptpd server – poptop document

psacct is known as acct in debian

If you are trying to install psacct in debian, you will get an error message:
E: Unable to locate package psacct
It is because it’s called acct in debian:
So, let do this:
apt-get install acct
But the way, I am still not getting used to:
aptitude install acct

Custom post look in wordpress – for code and pre tag

To custom the post look in wordpress especially for code and pre block, we can tweak the style.css, this is my code added at the end of style.css, you can follow “Appearance/Editor” to get there.
/* user edit */
pre {
border: solid 1px gray;
font-size: 0.8em;
margin: 0px;
padding: 4px;
background: #fafafa;
}
code {
font-size:0.8em;
color: #008099;
background: #f4f4f4;
padding: 4px;
}

custom css

Can’t do elevation under command line in windows 7

When I am running netstat from command line, it prompts:
C:\>netstat -anb
The requested operation requires elevation.

Then I realized there is a runas, so:

C:\>runas RUNAS USAGE:

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
/user: program

RUNAS [ [/noprofile | /profile] [/env] [/savecred] ]
/smartcard [/user:] program

RUNAS /trustlevel:program

/noprofile specifies that the user's profile should not be loaded.
This causes the application to load more quickly, but
can cause some applications to malfunction.
/profile specifies that the user's profile should be loaded.
This is the default.
/env to use current environment instead of user's.
/netonly use if the credentials specified are for remote
access only.
/savecred to use credentials previously saved by the user.
This option is not available on Windows 7 Home or Windows 7
Starter Editions
and will be ignored.
/smartcard use if the credentials are to be supplied from a
smartcard.
/user should be in form USER@DOMAIN or DOMAIN\USER
/showtrustlevels displays the trust levels that can be used as arguments
to /trustlevel.
/trustlevel should be one of levels enumerated
in /showtrustlevels.
program command line for EXE. See below for examples

Examples:
> runas /noprofile /user:mymachine\administrator cmd
> runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
> runas /env /user:user@domain.microsoft.com "notepad \"my file.txt\""

NOTE: Enter user's password only when prompted.
NOTE: /profile is not compatible with /netonly.
NOTE: /savecred is not compatible with /smartcard.

But unfortunately, it doesn’t help even if you run:
runas /user:administrator "netstat -abn"
I just want to get the same effort as right click and do “run as administrator”, seems that there is no command line equivalent to do this.
run as admin in menu

File auto backup in windows 7

Windows 7 comes with a nice feature, file auto backup. It is integrated into to the windows explorer, and is automatically turned on for c driver in a default installation. It will backup the files once everyday or when you create a backup point. This is pretty good for home use, keep your files safe even unnoticed.
previous versions
To restore to the previous version, right click on the file/folder to open properties dialog box, everything is in the “Previous Versoins“.

To turn on this feature for the other driver, for example: driver D, take these steps:

  1. Go to control panel: Control Panel\All Control Panel Items\System
  2. Click on System Protection to bring up System Properties dialog box:
    system properties
  3. You can see from screenshot, C driver is already turned on, to turn on driver D, select it and click the Configure button below:
    system protection
  4. Choose the restore settings and disk space, then OK, that’s it. And driver D is under auto backup watch, all the changes will be backup once per day or when a new backup point is made.

Computer Auto Shut Down 2.1 with command line support

Computer Auto Shut Down 2.1 has released with the support of command line interface, here is the command line paramter:
[-action poweroff | hibernate | sleep | logoff | reboot] [-t "minutes to shutdown"] [-help|-h]
For example to hibernate 30 minutes later please run:
autoshutdown -action hibernate -t 30