我正在学习Golang并正在编写一个代码片段,我将所有扩展ASCII字符打印到控制台。 我已经让我的循环从X80-XFF开始,它运行正常。但是,我想在循环输出的底部打印一行“€÷¾美元”,但我的代码只打印出我要打印的三个字符的十六进制+美元。谁能告诉我为什么这不起作用?
package ascii
import "fmt"
const Ascii =
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
+
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" +
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" +
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" +
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" +
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" +
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
func IterateOverASCIIStringLiteral(sample string) {
for i := 0; i < len(sample); i++ {
fmt.Printf("%X %q %b\n", sample[i], sample[i], sample[i])
}
}
func ExtendedASCIIText() {
fmt.Printf("%q","\x80\xf7\xbe dollar")
}
投入我的主要内容:
package main
import "./ascii"
func main() {
ascii.IterateOverASCIIStringLiteral(ascii.Ascii)
ascii.ExtendedASCIIText()
}
答案 0 :(得分:2)
有谁能告诉我为什么这不起作用?
是。你似乎不明白这个问题。术语扩展ASCII没有明确定义。您可能希望从(Windows)代码页1252转换为Unicode。
例如,
package main
import "fmt"
func cp1252ToUTF8(cp string) string {
r := make([]rune, len(cp))
for i := 0; i < len(cp); i++ {
r[i] = cp1252[cp[i]]
}
return string(r)
}
func main() {
cp := "\x80\xf7\xbe dollar"
str := cp1252ToUTF8(cp)
fmt.Printf("%q\n", str)
}
func init() {
for i, r := range cp1252 {
if r == 0 {
cp1252[i] = rune(i)
}
}
}
// cp1252 to Unicode table
// ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
var cp1252 = [256]rune{
0x80: '\u20AC', // EURO SIGN
0x81: '\uFFFD', // UNDEFINED
0x82: '\u201A', // SINGLE LOW-9 QUOTATION MARK
0x83: '\u0192', // LATIN SMALL LETTER F WITH HOOK
0x84: '\u201E', // DOUBLE LOW-9 QUOTATION MARK
0x85: '\u2026', // HORIZONTAL ELLIPSIS
0x86: '\u2020', // DAGGER
0x87: '\u2021', // DOUBLE DAGGER
0x88: '\u02C6', // MODIFIER LETTER CIRCUMFLEX ACCENT
0x89: '\u2030', // PER MILLE SIGN
0x8A: '\u0160', // LATIN CAPITAL LETTER S WITH CARON
0x8B: '\u2039', // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x8C: '\u0152', // LATIN CAPITAL LIGATURE OE
0x8D: '\uFFFD', // UNDEFINED
0x8E: '\u017D', // LATIN CAPITAL LETTER Z WITH CARON
0x8F: '\uFFFD', // UNDEFINED
0x90: '\uFFFD', // UNDEFINED
0x91: '\u2018', // LEFT SINGLE QUOTATION MARK
0x92: '\u2019', // RIGHT SINGLE QUOTATION MARK
0x93: '\u201C', // LEFT DOUBLE QUOTATION MARK
0x94: '\u201D', // RIGHT DOUBLE QUOTATION MARK
0x95: '\u2022', // BULLET
0x96: '\u2013', // EN DASH
0x97: '\u2014', // EM DASH
0x98: '\u02DC', // SMALL TILDE
0x99: '\u2122', // TRADE MARK SIGN
0x9A: '\u0161', // LATIN SMALL LETTER S WITH CARON
0x9B: '\u203A', // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x9C: '\u0153', // LATIN SMALL LIGATURE OE
0x9D: '\uFFFD', // UNDEFINED
0x9E: '\u017E', // LATIN SMALL LETTER Z WITH CARON
0x9F: '\u0178', // LATIN CAPITAL LETTER Y WITH DIAERESIS
}
游乐场:https://play.golang.org/p/U3dlRahOxQN
输出:
"€÷¾ dollar"
参考文献: