懒笔头

1.格式转化

  1. #include <sstream>
  2. stringstream ss;
  3. ss << "0x00fe" ;
  4. unsignal long  ulvalue;
  5. ss >> std::hex >> ulvalue;
  6. ss.str(""); //clear

2.UTF-8 和 Unicode 的关系

 

U-00000000 - U-0000007F:0xxxxxxx
U-00000080 - U-000007FF:110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF:1110xxxx 10xxxxxx 10xxxxxx
U-00010000 - U-001FFFFF:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 - U-03FFFFFF:111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

有一些2个字节的GBK字节会被当成UTF-8来解析,造成错误

GB2312-8,范围0xA1A1~0xFEFE

GBK包含GB2312-8,范围0x8140~0xFEFE(高位减去0x80,与Unicode2.0一一对应)

 

3.IP32地址有效性判断

template<typename T>
bool ValidIP32(const T& str)
{
        std::size_t i=0;
        std::size_t num=0;
        bool bstart = false;
        std::size_t iSc = 0;
        typedef T::value_type C;
        while(i<str.size())
        {
                if ((str[i] >= (C)'0') && (str[i] <= (C)'9'))
                {
                        bstart = true;
                        num *= 10;
                        num += str[i] - (C)'0';
                        if (num > 255) goto FAIL;
                }
                else
                if (str[i] == (C)'.')
                {
                        num = 0;
                       
                        if (!bstart) goto FAIL;
                        bstart = false;

                        iSc += 1;
                        if (iSc > 3) goto FAIL;
                }
                else
                if (str[i] != (C)' ') goto FAIL;
                ++i;
        }

        if (iSc != 3) goto FAIL;
        if (!bstart) goto FAIL;
        goto SUC;
FAIL: return false;
SUC: return true;
}

 

4.中文

fstream f;

f.imbuse(std::local("chs"));

 

 

5.Console和GUI的输入输出

#include "stdafx.h"
#include "xPipe2.h"

#include <iostream>
#include <sstream>
using namespace std;

#define MAX_LOADSTRING 100

#define _Hint(x) /
 ::MessageBox(0, x, L"xPipe2", MB_OK);


template<typename T, std::size_t N>
bool In(const T& a, T (&A)[N])
{
 for(std::size_t i=0; i<N; ++i)
 {
  if (a == A[i]) return true;
 }
 return false;
}


// =========================================================
// 全局变量:
HINSTANCE hInst;        // 当前实例
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 std::wcin.imbue(std::locale("chs"));
 std::wcout.imbue(std::locale("chs"));

 wstringstream xss;

 std::wstring str_temp;
 HANDLE hdl_input_origin(0);
 HANDLE hdl_output_origin(0);
 HANDLE hdl_buff1(0);

 // command line
 LPWSTR _CmdLine = ::GetCommandLine();
 _Hint(_CmdLine);

 // std_input_handle
 hdl_input_origin = ::GetStdHandle(STD_INPUT_HANDLE);
 hdl_output_origin = ::GetStdHandle(STD_OUTPUT_HANDLE);

 xss << L"Input:/t" << std::hex << hdl_input_origin << std::endl
  << L"Output:/t" << std::hex << hdl_output_origin;


 _Hint(xss.str().c_str());


 //::AllocConsole();

 //::SetLastError(0);
  必须在AllocConsole后才能成功创建buffer
 //hdl_buff1 = ::CreateConsoleScreenBuffer(GENERIC_WRITE,
 //  FILE_SHARE_WRITE, NULL,
 //  CONSOLE_TEXTMODE_BUFFER,
 //  NULL);
 //if (INVALID_HANDLE_VALUE == hdl_buff1)
 //{
 // DWORD dwError = ::GetLastError();
 // TRACE1("/ndwError:%x", dwError);
 //}
 HANDLE INVALID_OUTPUT_HANDLE[] ={(HANDLE)0x0, (HANDLE)0x7, INVALID_HANDLE_VALUE};
 HANDLE INVALID_INPUT_HANDLE[] ={(HANDLE)0x0, (HANDLE)0x3, INVALID_HANDLE_VALUE};

 if (!In(hdl_output_origin, INVALID_OUTPUT_HANDLE))
 {
  if (!In(hdl_input_origin, INVALID_INPUT_HANDLE))
  {
   const std::streamsize _Count = 1024;
   wstring _str(_Count, 0);
   while(!std::wcin.fail())
   {
    std::streamsize sst
     = std::wcin.get(const_cast<wchar_t*>(_str.c_str()), _Count, 0).gcount();
    if (_Count > sst) _str[sst] = 0;
    std::wcout << _str.c_str();
   }
   
   //std::wcout << L"output";
  }
 }
 else _Hint(L"INVALID_HANDLE");

 return 0;
}

 

参考

http://support.microsoft.com/kb/105305/en-us/

 

6.MIME

http://dev.csdn.net/article/18/18448.shtm

 

7.iframe自适应高度

file frm.html

<html>
<head>
<style type="text/css">
.autofit{height: expression(frames("ifr2").document.body.scrollHeight);}

</style>
<script language=javascript>
</script>
</head>
<body>
main

<iframe name="ifr" οnlοad="document.all['ifr'].style.height=ifr.document.body.scrollHeight" scrolling="no" width="100%" src="emb.html"></iframe>
<iframe name="ifr2" class="autofit" scrolling="no" width="100%" src="emb.html"></iframe>
<iframe name="ifr3" style="height:expression(document.ifr3.document.body.scrollHeight)" scrolling="no" width="100%" src="emb.html"></iframe>
<iframe name="ifr4" style="height:expression(window.frames('ifr4').document.body.scrollHeight)" scrolling="no" width="100%" src="emb.html"></iframe>
</body>
</html>

 

file emb.html

<html>
<body>
<a href="#" οnclick="document.getElementById('oy').style.display = 'block'">show</a>
<a href="#" οnclick="document.getElementById('oy').style.display = 'none'">hide</a>
embbed
<div id="oy" name="oy" style="display: block"><img src="http://d4.sina.com.cn/201005/08/224685_140x280.jpg"></div>
after div

</body>
</html>

 

8.LUA api

lua_topointer

[-0, +0, - ]

const void *lua_topointer (lua_State *L, int index);

Converts the value at the given acceptable index to a generic C pointer (void* ). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returnsNULL . Different objects will give different pointers. There is no way to convert the pointer back to its original value.

Typically this function is used only for debug information.

 

9.C++ stream iterator back_inserter function

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
 std::vector<int> v;
 std::istream_iterator<int> cin_int(std::cin);
 std::istream_iterator<int> cin_eof;
 copy(cin_int, cin_eof, back_inserter(v));
 copy(v.begin(), v.end(), ostream_iterator<int>(std::cout, " "));
 return 0;
}


10.WScript Shell SendKeys 打开输入法输出中文

set sh=WScript.CreateObject("WScript.Shell")
WScript.Sleep 300
sh.SendKeys "{ENTER}"
'sh.SendKeys "{CAPSLOCK}"
sh.SendKeys "type con > aaa.txt{ENTER}"
sh.SendKeys "abcdefg~"

sh.SendKeys "^ "
sh.SendKeys "iiii khyy ~"

WScript.Sleep 300
sh.SendKeys "^ "
sh.SendKeys "^(z)~"
'sh.SendKeys "{CAPSLOCK}"


11.Code::Blocks lua local ip

-Wl,--enable-auto-import

-liphlpapi

#include <iostream>
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <windows.h>
#include <Iptypes.h>
#include <iphlpapi.h>
#include <winsock2.h>

using namespace std;

int main()
{
    string strResult;
    IP_ADAPTER_INFO *pAI = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    if (GetAdaptersInfo(pAI, &ulOutBufLen)==ERROR_BUFFER_OVERFLOW)
    {
        free(pAI);
        pAI=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
    }
    DWORD dwRet = ::GetAdaptersInfo(pAI, &ulOutBufLen);
    if (ERROR_SUCCESS == dwRet)
    {
        _IP_ADDR_STRING *p = &pAI->IpAddressList;
        do {
            strResult += p->IpAddress.String;
            strResult += " ";
        }while(p=p->Next);
        //strResult = pAI->CurrentIpAddress->IpAddress.String;
    }
    free(pAI);

    std::cout << strResult << std::endl;

    lua_State* L = lua_open();
    luaL_openlibs(L);
    std::cout << std::hex << L << std::endl;
    luaL_dostring(L, "print(1*2*3*4*5*6);");
    lua_close(L);

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值