popraw poniższy kod źródłowy aby nie występował ten błąd: Examples of 128-bit of data in…
- User
popraw poniższy kod źródłowy aby nie występował ten błąd:
Examples of 128-bit of data in B256U Encoding
[; ð X í◄►Q9x ~ ê 1@] panic: check failed
goroutine 1 [running]: main.main() c:/GO/base256/b256.6.go:75 +0x212
package main
import ( "bytes" "fmt" "math/rand" "reflect" "time" )
var alphabetB256U [256]rune = [256]rune{ 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, }
func encodeB256U(dataPtr []byte) string { var buf bytes.Buffer for _, v := range dataPtr { character := rune(v) if character >= 128 { buf.WriteRune(alphabetB256U[v]) buf.WriteRune(alphabetB256U[v-128]) } else { buf.WriteRune(character) } } return buf.String() }
func decodeB256U(str string) []byte { result := make([]byte, 0, len(str)/2) // zmieniony rozmiar w celu uniknięcia błędu indeksu poza zakresem dataPtr := 0 var unicode rune for i := 0; i < len(str); i += 2 { code1 := rune(str[i]) if i+1 < len(str) { code2 := rune(str[i+1]) if code1 >= 128 && code2 >= 128 { unicode = (code1-192)<<6 | (code2-128)&63 result = append(result, byte(unicode)) } else { result = append(result, byte(code1)) i-- // cofnij się o jeden indeks, aby powtórzyć dekodowanie dla tego samego znaku } } else { result = append(result, byte(code1)) } dataPtr++ } return result }
func randomize(dataPtr []byte) { rand.Seed(time.Now().Unix()) for i := 0; i < len(dataPtr); i++ { dataPtr[i] = byte(rand.Int()) } }
func main() { fmt.Println() fmt.Println("Examples of 128-bit of data in B256U Encoding") fmt.Println("---------------------------------------------") for i := 0; i < 100; i++ { binaryData := make([]byte, 128/8) randomize(binaryData)
text := encodeB256U(binaryData) fmt.Printf("[%s]\t", text) decodedData := decodeB256U(text) if !reflect.DeepEqual(binaryData, decodedData) { panic("check failed") } }}
- Assistant
Poprawiony kod:
package main import ( "bytes" "fmt" "math/rand" "reflect" "time" ) var alphabetB256U [256]rune = [256]rune{ 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, } func encodeB256U(dataPtr []byte) string { var buf bytes.Buffer for _, v := range dataPtr { character := rune(v) if character >= 128 { buf.WriteRune(alphabetB256U[v]) buf.WriteRune(alphabetB256U[v-128]) } else { buf.WriteByte(v) // zmiana WriteRune na WriteByte } } return buf.String() } func decodeB256U(str string) []byte { result := make([]byte, 0, len(str)/2) dataPtr := 0 var unicode rune for i := 0; i < len(str); i += 2 { code1 := rune(str[i]) if i+1 < len(str) { code2 := rune(str[i+1]) if code1 >= 128 && code2 >= 128 { unicode = (code1-192)<<6 | (code2-128)&63 result = append(result, byte(unicode)) } else { result = append(result, byte(code1)) i-- // cofnij się o jeden indeks, aby powtórzyć dekodowanie dla tego samego znaku } } else { result = append(result, byte(code1)) } dataPtr++ } return result } func randomize(dataPtr []byte) { rand.Seed(time.Now().Unix()) for i := 0; i < len(dataPtr); i++ { dataPtr[i] = byte(rand.Int()) } } func main() { fmt.Println() fmt.Println("Examples of 128-bit of data in B256U Encoding") fmt.Println("---------------------------------------------") for i := 0; i < 100; i++ { binaryData := make([]byte, 128/8) randomize(binaryData) text := encodeB256U(binaryData) fmt.Printf("[%s]\t", text) decodedData := decodeB256U(text) if !reflect.DeepEqual(binaryData, decodedData) { panic("check failed") } } } - User
popraw o błąd: Examples of 128-bit of data in B256U Encoding
[ Ú ßP∟ ßkS MI♫ õ] panic: check failed
goroutine 1 [running]: main.main() c:/GO/base256/b256.6.go:70 +0x338
- Assistant
Po analizie błędu, wygląda na to, że wartości znaków unikodowych w są niepoprawne. Poprawione wartości to: