golang检测字节是否为utf-8字节的起始字节

原理 UTF-8 编码使用不同长度的字节序列来表示 Unicode 字符。这些序列的长度从一个字节到四个字节不等,每个字节都有特定的比特模式。您可以通过观察任何字节的最高位(也就是最左边的几位比特),来判断这个字节是不是某个字符的 UTF-8 编码的起始字节或中间字节: 单字节字符(U+0000 到 U+007F)以 0xxxxxxx 为模式,其中 x 可以是 0 或 1。这种单字节的高位为 0,表示它是 ASCII 字符的起始(也是唯一)字节。 UTF-8 编码的起始字节(即多字节序列的第一个字节)模式为 110xxxxx(对于两字节编码)、1110xxxx(对于三字节编码)或 11110xxx(对于四字节编码)。 对于一个 UTF-8 字符的非起始字节(也称为连续字节或中间字节),其模式为 10xxxxxx。 因此,如果你看到一个字节,它的最高两位是 10,那么它是 UTF-8 编码中的一个连续字节。如果最高一位是 0 或者高位模式匹配 110、1110 或 11110,它就是起始字节。 举例来说: 0xxxxxxx - UTF-8 字符的起始字节(单字节 ASCII 字符) 110xxxxx - 两字节编码的起始字节 1110xxxx - 三字节编码的起始字节 11110xxx - 四字节编码的起始字节 10xxxxxx - 中间字节 通过检查字节的最高位,您就可以迅速确定它是 UTF-8 编码序列的起始字节还是中间字节。 实现 go语言实现 package main import ( "fmt" ) // isUTF8StartByte checks whether a byte is a UTF-8 start byte. func isUTF8StartByte(b byte) bool { // For single-byte (ASCII), starts with 0xxxxxxx if b&0x80 == 0x00 { return true } // For multibyte sequences, if a byte starts with // 110xxxxx, 1110xxxx, or 11110xxx, it is a start byte. if (b&0xE0 == 0xC0) || (b&0xF0 == 0xE0) || (b&0xF8 == 0xF0) { return true } // Otherwise, it is a continuation byte (10xxxxxx). return false } func main() { examples := []byte{0b01110100, 0b11011110, 0b11101110, 0b10111100, 0b00111100} for _, b := range examples { if isUTF8StartByte(b) { fmt.Printf("Byte 0x%X is a start byte of a UTF-8 character.\n", b) } else { fmt.Printf("Byte 0x%X is a continuation or invalid byte of a UTF-8 character.\n", b) } } }

2024-02-19 · 1 分钟 · czyt

如何使用C#检查网络连接

调用Windows API 在C#中可以通过Pinvoke调用windows API方式来进行网络连接的检查: [DllImport("wininet.dll", SetLastError=true)] extern static bool InternetGetConnectedState(out int lpdwFlags, int dwReserved); [Flags] enum ConnectionStates { Modem = 0x1, LAN = 0x2, Proxy = 0x4, RasInstalled = 0x10, Offline = 0x20, Configured = 0x40, } MSDN地址 https://learn.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetgetconnectedstate 对应的返回值Description如下: Value Meaning INTERNET_CONNECTION_CONFIGURED0x40 Local system has a valid connection to the Internet, but it might or might not be currently connected. INTERNET_CONNECTION_LAN0x02 Local system uses a local area network to connect to the Internet. INTERNET_CONNECTION_MODEM0x01 Local system uses a modem to connect to the Internet. INTERNET_CONNECTION_MODEM_BUSY0x08 No longer used. INTERNET_CONNECTION_OFFLINE0x20 Local system is in offline mode. INTERNET_CONNECTION_PROXY0x04 Local system uses a proxy server to connect to the Internet. INTERNET_RAS_INSTALLED0x10 Local system has RAS installed. 例子 : ...

2023-08-31 · 2 分钟 · czyt

Golang监测Linux网络事件

代码 package main import ( "fmt" "syscall" ) func main() { l, _ := ListenNetlink() for { msgs, err := l.ReadMsgs() if err != nil { fmt.Println("Could not read netlink: %s", err) } for _, m := range msgs { if IsNewAddr(&m) { fmt.Println("New Addr") } if IsDelAddr(&m) { fmt.Println("Del Addr") } } } } type NetlinkListener struct { fd int sa *syscall.SockaddrNetlink } func ListenNetlink() (*NetlinkListener, error) { groups := (1 << (syscall.RTNLGRP_LINK - 1)) | (1 << (syscall.RTNLGRP_IPV4_IFADDR - 1)) | (1 << (syscall.RTNLGRP_IPV6_IFADDR - 1)) s, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, syscall.NETLINK_ROUTE) if err != nil { return nil, fmt.Errorf("socket: %s", err) } saddr := &syscall.SockaddrNetlink{ Family: syscall.AF_NETLINK, Pid: uint32(0), Groups: uint32(groups), } err = syscall.Bind(s, saddr) if err != nil { return nil, fmt.Errorf("bind: %s", err) } return &NetlinkListener{fd: s, sa: saddr}, nil } func (l *NetlinkListener) ReadMsgs() ([]syscall.NetlinkMessage, error) { defer func() { recover() }() pkt := make([]byte, 2048) n, err := syscall.Read(l.fd, pkt) if err != nil { return nil, fmt.Errorf("read: %s", err) } msgs, err := syscall.ParseNetlinkMessage(pkt[:n]) if err != nil { return nil, fmt.Errorf("parse: %s", err) } return msgs, nil } func IsNewAddr(msg *syscall.NetlinkMessage) bool { if msg.Header.Type == syscall.RTM_NEWADDR { return true } return false } func IsDelAddr(msg *syscall.NetlinkMessage) bool { if msg.Header.Type == syscall.RTM_DELADDR { return true } return false } // rtm_scope is the distance to the destination: // // RT_SCOPE_UNIVERSE global route // RT_SCOPE_SITE interior route in the // local autonomous system // RT_SCOPE_LINK route on this link // RT_SCOPE_HOST route on the local host // RT_SCOPE_NOWHERE destination doesn't exist // // The values between RT_SCOPE_UNIVERSE and RT_SCOPE_SITE are // available to the user. func IsRelevant(msg *syscall.IfAddrmsg) bool { if msg.Scope == syscall.RT_SCOPE_UNIVERSE || msg.Scope == syscall.RT_SCOPE_SITE { return true } return false } 参考 https://stackoverflow.com/questions/36347807/how-to-monitor-ip-address-change-using-rtnetlink-socket-in-go-language ...

2022-05-17 · 2 分钟 · czyt

golang检测网络连接是否关闭

_, err := conn.Read(make([]byte, 0)) if err!=io.EOF{ // this connection is invalid logger.W("conn closed....",err) }else{ byt, _:= ioutil.ReadAll(conn); } 注意:net: don’t return io.EOF from zero byte reads issue 参考 https://stackoverflow.com/questions/12741386/how-to-know-tcp-connection-is-closed-in-net-package

2020-06-11 · 1 分钟 · czyt