本文整理汇总了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
}
示例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)
}
示例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})
}
示例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))
}
示例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")
}
}
示例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
}
示例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
}
}
示例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
}
示例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))
}
示例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)
}
示例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))
}
示例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
}
示例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))
}
示例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]
}
示例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))
}