當前位置: 首頁>>代碼示例>>Golang>>正文


Golang utf16.Decode函數代碼示例

本文整理匯總了Golang中unicode/utf16.Decode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Decode函數的具體用法?Golang Decode怎麽用?Golang Decode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Decode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: get_string

func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
	if w.Is5ver {
		var bts = make([]byte, size)
		_, err = buf.Read(bts)
		res = string(bts)
	} else {
		var richtext_num uint16
		var phonetic_size uint32
		var flag byte
		err = binary.Read(buf, binary.LittleEndian, &flag)
		if flag&0x8 != 0 {
			err = binary.Read(buf, binary.LittleEndian, &richtext_num)
		}
		if flag&0x4 != 0 {
			err = binary.Read(buf, binary.LittleEndian, &phonetic_size)
		}
		if flag&0x1 != 0 {
			var bts = make([]uint16, size)
			var i = uint16(0)
			for ; i < size && err == nil; i++ {
				err = binary.Read(buf, binary.LittleEndian, &bts[i])
			}
			runes := utf16.Decode(bts[:i])
			res = string(runes)
			if i < size {
				w.continue_utf16 = size - i
			}
		} else {
			var bts = make([]byte, size)
			var n int
			n, err = buf.Read(bts)
			if uint16(n) < size {
				w.continue_utf16 = size - uint16(n)
				err = io.EOF
			}

			var bts1 = make([]uint16, size)
			for k, v := range bts[:n] {
				bts1[k] = uint16(v)
			}
			runes := utf16.Decode(bts1)
			res = string(runes)
		}
		if flag&0x8 != 0 {
			var bts []byte
			if w.Is5ver {
				bts = make([]byte, 2*richtext_num)
			} else {
				bts = make([]byte, 4*richtext_num)
			}
			err = binary.Read(buf, binary.LittleEndian, bts)
		}
		if flag&0x4 != 0 {
			var bts []byte
			bts = make([]byte, phonetic_size)
			err = binary.Read(buf, binary.LittleEndian, bts)
		}
	}
	return
}
開發者ID:tfzxyinhao,項目名稱:xls,代碼行數:60,代碼來源:workbook.go

示例2: builtin_unescape

func builtin_unescape(input string) string {
	output := make([]rune, 0, len(input))
	length := len(input)
	for index := 0; index < length; {
		if input[index] == '%' {
			if index <= length-6 && input[index+1] == 'u' {
				byte16, err := hex.DecodeString(input[index+2 : index+6])
				if err == nil {
					value := uint16(byte16[0])<<8 + uint16(byte16[1])
					chr := utf16.Decode([]uint16{value})[0]
					output = append(output, chr)
					index += 6
					continue
				}
			}
			if index <= length-3 {
				byte8, err := hex.DecodeString(input[index+1 : index+3])
				if err == nil {
					value := uint16(byte8[0])
					chr := utf16.Decode([]uint16{value})[0]
					output = append(output, chr)
					index += 3
					continue
				}
			}
		}
		output = append(output, rune(input[index]))
		index += 1
	}
	return string(output)
}
開發者ID:NetSys,項目名稱:quilt,代碼行數:31,代碼來源:builtin.go

示例3: Read

func Read(charset string, data []byte) string {
	switch charset {
	case "utf8":
		var runes []rune
		for i := 0; i < len(data); {
			r, size := utf8.DecodeRune(data[i:])
			if r != utf8.RuneError {
				runes = append(runes, r)
				i += size
			}
		}
		return string(runes)
	case "utf16le":
		uint16s := make([]uint16, len(data)/2)
		for i := 0; i < len(uint16s); i++ {
			uint16s[i] = binary.LittleEndian.Uint16(data[i*2:])
		}
		return string(utf16.Decode(uint16s))
	case "utf16be":
		uint16s := make([]uint16, len(data)/2)
		for i := 0; i < len(uint16s); i++ {
			uint16s[i] = binary.BigEndian.Uint16(data[i*2:])
		}
		return string(utf16.Decode(uint16s))
	}
	return string([]rune{replacementChar})
}
開發者ID:xsoameix,項目名稱:utfconv,代碼行數:27,代碼來源:utfconv.go

示例4: toString

func toString(value Value) string {
	if value._valueType == valueString {
		switch value := value.value.(type) {
		case string:
			return value
		case []uint16:
			return string(utf16.Decode(value))
		}
	}
	if value.IsUndefined() {
		return "undefined"
	}
	if value.IsNull() {
		return "null"
	}
	switch value := value.value.(type) {
	case bool:
		return strconv.FormatBool(value)
	case int:
		return strconv.FormatInt(int64(value), 10)
	case int8:
		return strconv.FormatInt(int64(value), 10)
	case int16:
		return strconv.FormatInt(int64(value), 10)
	case int32:
		return strconv.FormatInt(int64(value), 10)
	case int64:
		return strconv.FormatInt(value, 10)
	case uint:
		return strconv.FormatUint(uint64(value), 10)
	case uint8:
		return strconv.FormatUint(uint64(value), 10)
	case uint16:
		return strconv.FormatUint(uint64(value), 10)
	case uint32:
		return strconv.FormatUint(uint64(value), 10)
	case uint64:
		return strconv.FormatUint(value, 10)
	case float32:
		if value == 0 {
			return "0" // Take care not to return -0
		}
		return floatToString(float64(value), 32)
	case float64:
		if value == 0 {
			return "0" // Take care not to return -0
		}
		return floatToString(value, 64)
	case []uint16:
		return string(utf16.Decode(value))
	case string:
		return value
	case *_object:
		return toString(value.DefaultValue(defaultValueHintString))
	}
	panic(fmt.Errorf("toString(%v %T)", value.value, value.value))
}
開發者ID:couchbaselabs,項目名稱:otto,代碼行數:57,代碼來源:value_string.go

示例5: getch

func getch() (byte, error) {
	modkernel32 := syscall.NewLazyDLL("kernel32.dll")
	procReadConsole := modkernel32.NewProc("ReadConsoleW")
	procGetConsoleMode := modkernel32.NewProc("GetConsoleMode")
	procSetConsoleMode := modkernel32.NewProc("SetConsoleMode")

	var mode uint32
	pMode := &mode
	procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode)))

	var echoMode, lineMode uint32
	echoMode = 4
	lineMode = 2
	var newMode uint32
	newMode = mode &^ (echoMode | lineMode)

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode))
	defer procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode))

	line := make([]uint16, 1)
	pLine := &line[0]
	var n uint16
	procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n)))

	b := []byte(string(utf16.Decode(line)))

	// Not sure how this could happen, but it did for someone
	if len(b) > 0 {
		return b[0], nil
	} else {
		return 13, errors.New("Read error")
	}
}
開發者ID:Machyne,項目名稱:mongo,代碼行數:33,代碼來源:win.go

示例6: readBytes

func readBytes(b io.Reader, values ...interface{}) error {
	for _, v := range values {
		_, ok := v.(*string)
		if ok {
			var size int16
			err := binary.Read(b, binary.BigEndian, &size)
			if err != nil {
				return err
			}
			raw := make([]uint16, int(size))
			for i := int16(0); i < size; i++ {
				var ch int16
				err = binary.Read(b, binary.BigEndian, &ch)
				if err != nil {
					return err
				}
				raw[int(i)] = uint16(ch)
			}

			s := string(utf16.Decode(raw))
			reflect.ValueOf(v).Elem().Set(reflect.ValueOf(s))
		} else {
			err := binary.Read(b, binary.BigEndian, v)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
開發者ID:jeffh,項目名稱:mc,代碼行數:30,代碼來源:testing_utils.go

示例7: getch

func getch() byte {
	modkernel32 := syscall.NewLazyDLL("kernel32.dll")
	procReadConsole := modkernel32.NewProc("ReadConsoleW")
	procGetConsoleMode := modkernel32.NewProc("GetConsoleMode")
	procSetConsoleMode := modkernel32.NewProc("SetConsoleMode")

	var mode uint32
	pMode := &mode
	procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode)))

	var echoMode, lineMode uint32
	echoMode = 4
	lineMode = 2
	var newMode uint32
	newMode = mode ^ (echoMode | lineMode)

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode))

	line := make([]uint16, 1)
	pLine := &line[0]
	var n uint16
	procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n)))

	b := []byte(string(utf16.Decode(line)))

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode))

	if len(b) > 0 {
		return b[0]
	} else {
		return 13
	}
}
開發者ID:jjz,項目名稱:ztodo,代碼行數:33,代碼來源:win.go

示例8: getKeys

func getKeys() []keyInfo {
	var numberOfEventsRead uint32
	var events [10]inputRecordT
	var orgConMode uint32

	result := make([]keyInfo, 0, 0)

	getConsoleMode.Call(uintptr(hConin),
		uintptr(unsafe.Pointer(&orgConMode)))
	setConsoleMode.Call(uintptr(hConin), 0)
	readConsoleInput.Call(
		uintptr(hConin),
		uintptr(unsafe.Pointer(&events[0])),
		uintptr(len(events)),
		uintptr(unsafe.Pointer(&numberOfEventsRead)))
	setConsoleMode.Call(uintptr(hConin), uintptr(orgConMode))
	for i := uint32(0); i < numberOfEventsRead; i++ {
		if events[i].eventType == KEY_EVENT && events[i].bKeyDown != 0 {
			var keycode rune
			if events[i].unicodeChar == 0 {
				keycode = rune(0)
			} else {
				keycode = utf16.Decode([]uint16{events[i].unicodeChar})[0]
			}
			result = append(result, keyInfo{
				keycode,
				events[i].wVirtualKeyCode,
				events[i].dwControlKeyState,
			})
		}
	}
	return result
}
開發者ID:kissthink,項目名稱:nyagos,代碼行數:33,代碼來源:getch.go

示例9: utf16Decode

func utf16Decode(s string) string {
	var u []uint16
	for i := 0; i < len(s); i += 2 {
		u = append(u, uint16(s[i])<<8|uint16(s[i+1]))
	}
	return string(utf16.Decode(u))
}
開發者ID:Totalboy,項目名稱:pdf,代碼行數:7,代碼來源:text.go

示例10: main

func main() {

	if len(os.Args) != 2 {

		fmt.Println("Usage: ", os.Args[0], "host:port")

		os.Exit(1)

	}

	service := os.Args[1]

	conn, err := net.Dial("tcp", service)

	checkError(err)

	shorts := readShorts(conn)

	ints := utf16.Decode(shorts)

	str := string(ints)

	fmt.Println(str)

	os.Exit(0)

}
開發者ID:sjtlqy,項目名稱:Go,代碼行數:27,代碼來源:utf16_client.go

示例11: ReadString

func ReadString(stream io.Reader) string {
	var length uint16
	binary.Read(stream, binary.BigEndian, &length)
	var data = make([]uint16, length)
	binary.Read(stream, binary.BigEndian, data)
	return string(utf16.Decode(data))
}
開發者ID:mechmind,項目名稱:diggo,代碼行數:7,代碼來源:wire.go

示例12: Read

func (minecraftType *String) Read(rawBytes io.Reader) error {
	err := binary.Read(rawBytes, binary.BigEndian, &minecraftType.MessageLength)
	if err != nil {
		return err
	}

	if minecraftType.MessageLength > 0 {
		// The problem is that Messagelength is the size of 2 byte words and we are working
		// with bytes. Therefore, we have to double the length to get the actual size. Do not
		// convert the bytes to strings unless needed.
		minecraftType.Raw = make([]byte, (minecraftType.MessageLength * 2))
		if _, err := rawBytes.Read(minecraftType.Raw); err != nil {
			return err
		}
		var utf16Message []uint16 = make([]uint16, minecraftType.MessageLength)
		for at := int16(0); at < minecraftType.MessageLength; at++ {
			var firstByte int16 = at * int16(2)
			var secondByte int16 = firstByte + int16(2)
			characterBytes := minecraftType.Raw[firstByte:secondByte]
			utf16Message[at] = binary.BigEndian.Uint16(characterBytes)
		}
		minecraftType.Message = string(utf16.Decode(utf16Message))
	}
	return nil
}
開發者ID:DragonU,項目名稱:minecraft-protocol,代碼行數:25,代碼來源:string.go

示例13: StringUtf16

func (dec *FrameDecoder) StringUtf16() string {
	strLen := int(dec.Int32())
	if dec.Err != nil {
		return ""
	}
	if strLen < 0 {
		dec.Err = ErrLengthTooLarge
	}

	// Deliberately extract the string bytes here, MaxFrameLen implicitly
	// protects against allocating overly long strings.
	data := dec.Bytes(strLen * 2)
	if data == nil {
		return ""
	}

	strWords := make([]uint16, strLen)
	offset := 0
	nullIndex := strLen
	for i := range strWords {
		strWords[i] = endianness.Uint16(data[offset : offset+2])
		if strWords[i] == 0 && i < nullIndex {
			nullIndex = i
			strWords = strWords[:i]
			break
		}
		offset += 2
	}

	return string(utf16.Decode(strWords))
}
開發者ID:huin,項目名稱:artemis,代碼行數:31,代碼來源:framedec.go

示例14: parseUTF16String

// parseUTF16String parses and returns a UTF-16 NULL-Terminated
// string from b.
func parseUTF16String(b []uint16) string {
	s := string(utf16.Decode(b))

	// The following will only return the portion of the string
	// that occurs before the first NULL byte.
	return strings.SplitN(s, "\x00", 2)[0]
}
開發者ID:jtsylve,項目名稱:DFRWS-Go-Workshop,代碼行數:9,代碼來源:recycler.go

示例15: readBlockString

func readBlockString(off int, data []byte) string {
	// VERY inefficient, just for testing
	//buf := make([]byte, 0, 20)
	var offset uint16
	var length uint16

	offset = (uint16(data[off+1]) * 256) + uint16(data[off+0])
	length = (uint16(data[off+3]) * 256) + uint16(data[off+2])

	//Hackish:
	offset -= 36

	//errLog.Printf("off: %v, Length: %v, offset %v\n", off, length, offset)
	//x := bytes.NewReader(data[offset : offset+length])
	x := data[offset : offset+(length*2)]
	//errLog.Printf("off: %v, Length: %v, offset %v, XX: % x \n", off, length, offset, x)

	//var y []uint16
	y := make([]uint16, 0)

	errLog.Printf("%v", length)
	for i := 0; i < int(length); i++ {
		var xd uint16
		i2 := i * 2
		//err = binary.Read(x, binary.LittleEndian, xd)
		xd = (uint16(x[i2+1]) >> 8) + uint16(x[i2])
		//errLog.Printf("b: %x, c: %v", xd, string(rune(x[i2])))
		y = append(y, xd)
	}

	return string(utf16.Decode(y))
}
開發者ID:Grovespaz,項目名稱:go-tds,代碼行數:32,代碼來源:conn_test.go


注:本文中的unicode/utf16.Decode函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。