Skip to content

Instantly share code, notes, and snippets.

View huihut's full-sized avatar
🌴
On vacation

huihut huihut

🌴
On vacation
View GitHub Profile
@huihut
huihut / UWPGetInfo.cs
Last active November 22, 2018 12:13
UWP get Device, OS and App info
/*
https://www.suchan.cz/2015/08/uwp-quick-tip-getting-device-os-and-app-info/
https://docs.microsoft.com/zh-cn/windows/desktop/api/winnt/ns-winnt-_osversioninfoa
https://social.msdn.microsoft.com/Forums/WINDOWS/en-US/6d754895-36c0-403d-a91d-f0efbc1f36a8/uwphow-to-retrieve-os-version-in-a-universal-app?forum=wpdevelop
*/
using Windows.ApplicationModel;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.System.Profile;
...
@huihut
huihut / MD5_SHA1_encrypt.cs
Created October 26, 2018 10:24
C# MD5 and SHA1 encrypt
// MD5
public static string Md5(string str)
{
var algorithm = HashAlgorithmNames.Md5;
HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(algorithm);
var hash = provider.CreateHash();
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf16LE);
hash.Append(buffer);
var hashedBuffer = hash.GetValueAndReset();
return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16LE, hashedBuffer);
@huihut
huihut / Set_WebSocket_timeout.cs
Created September 20, 2018 12:48
Set WebSocket timeout
// set websocket url
string ip = "ws://" + "api.test.com:80";
// new websocket
MessageWebSocket socket = new MessageWebSocket();
var cts = new CancellationTokenSource();
cts.CancelAfter(2000); // cancel after 2 seconds
var connectAsync = socket.ConnectAsync(new Uri(ip));
var connectTask = connectAsync.AsTask(cts.Token);
@huihut
huihut / UTF8_To_Std_Str_and_UTF8_To_Managed_Str.cpp
Last active September 18, 2018 09:40
WinRT(C++/CX) UTF8_To_Std_Str and UTF8_To_Managed_Str
#include <string>
#include <Windows.h>
std::string UTF8_To_Std_Str(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1];
memset(pwBuf, 0, nwLen * 2 + 2);
@huihut
huihut / set_query_parameter.py
Created August 28, 2018 15:45
set web url query parameter
def set_query_parameter(url, param_name, param_value):
"""Given a URL, set or replace a query parameter and return the
modified URL.
>>> set_query_parameter('http://example.com?foo=bar&biz=baz', 'foo', 'stuff')
'http://example.com?foo=stuff&biz=baz'
"""
scheme, netloc, path, query_string, fragment = urlsplit(url)
query_params = parse_qs(query_string)
@huihut
huihut / encodeConversion.py
Created August 28, 2018 15:42
Encode Conversion
def encodeConversion(req):
if req.encoding == 'ISO-8859-1':
encodings = requests.utils.get_encodings_from_content(req.text)
if encodings:
encoding = encodings[0]
else:
encoding = req.apparent_encoding
# encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
encode_content = req.content.decode(encoding, 'replace') # 如果设置为replace,则会用?取代非法字符;
@huihut
huihut / guess_encoding.py
Created August 28, 2018 15:40
Guess the encoding of the file
def guess_encoding(csv_file):
"""guess the encoding of the given file"""
import io
import locale
with io.open(csv_file, "rb") as f:
data = f.read(5)
if data.startswith(b"\xEF\xBB\xBF"): # UTF-8 with a "BOM"
return "utf-8-sig"
elif data.startswith(b"\xFF\xFE") or data.startswith(b"\xFE\xFF"):
return "utf-16"
@huihut
huihut / VS2017-Professional-Enterprise-Key.txt
Created August 2, 2018 09:26
VS2017 专业版(Professional)和企业版(Enterprise)激活密钥
专业版(Professional):
KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
企业版(Enterprise):
NJVYC-BMHX2-G77MM-4XJMR-6Q8QF
@huihut
huihut / CodingInterviews005-从尾到头打印链表.cpp
Last active July 19, 2018 07:33
CodingInterviews005-从尾到头打印链表
/*
题目:从尾到头打印链表
题目描述:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
*/
/**
* struct ListNode {
* int val;
@huihut
huihut / create_csv.py
Last active June 17, 2018 08:16
Python 生成 CSV 文件,可用于生成带标签的数据集 CSV 文件,标签从0开始自动升序:0,1,2,3...
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Python 生成 CSV 文件
# Python 生成 CSV 文件,可用于生成带标签的数据集 CSV 文件,标签从0开始自动升序:0,1,2,3...
# 作者:https://github.com/huihut
# 参考:https://github.com/opencv/opencv_attic/blob/master/opencv/modules/contrib/doc/facerec/src/create_csv.py
'''