英文:
Decompression of string not works properly
问题
我正在编写代码来准备一个用于我的QR码的压缩编码字符串。我使用TextEncoder
将我的数据编码为Uint8Array,然后使用brotli库进一步压缩以减小大小。
在某个时候,我想将压缩编码的字符串连接到QR码中,以获得随机值,我使用TextDecoder
解码它,并使用随机字符串值来构建我的QR码。问题是,解码后得到的字符串,如果我执行编码 - 解压缩 - 解码操作,我无法恢复到原始字符串。
const encodedDetails = new TextEncoder().encode(JSON.stringify(this.studentDetails, null, 0)); // 编码为Uint8Array
const compressedEncodedDetails = await compress(encodedDetails); // 将Uint8Array压缩为Uint8Array
const decodeData = new TextDecoder().decode(compressedEncodedDetails); // 解码Uint8Array为字符串以创建QR码
const encode_decodeData = new TextEncoder().encode(decodeData); // 编码字符串为Uint8Array
const de_compress_compressedData = await decompress(encode_decodeData); // 解压缩
const orignalData = new TextDecoder().decode(de_compress_compressedData); // 解码原始值
console.log('+++++ 编码数据 1', encodedDetails);
console.log('+++++ 压缩数据 2', compressedEncodedDetails);
console.log('++++++ 解码数据 3', decodeData);
console.log('++++++ 编码解码数据 4', encode_decodeData);
console.log('++++++ 解压缩压缩数据 5', de_compress_compressedData);
console.log('++++++ 原始数据 6', orignalData);
我真的不明白问题出在哪里,任何提示或建议都将有益。
以下是控制台日志:
英文:
I am writing a code to prepare a compressed encoded string for my qr-code. I am using TextEncoder
for encoding of my data into Uint8Array and further compressing to reduce the size using the brotli library.
At some point I want to concatenate the compressed encoded string into the qr-code to have random value for which I am decoding it using TextDecoder
and using the random string value to build my QR-code. The problem is that the string I get after decoding I am unable to get to the original string If i perform encode -> decompress - decode.
const encodedDetails = new TextEncoder().encode(JSON.stringify(this.studentDetails, null, 0)); // encoded in Uint8Array
const compressedEncodedDetails = await compress(encodedDetails); // compress Uint8Array into Uint8Array
const decodeData = new TextDecoder().decode(compressedEncodedDetails); // decoding Uint8Array into string to create QR code
const encode_decodeData = new TextEncoder().encode(decodeData); // encoding string into Uint8Array
const de_compress_compressedData = await decompress(encode_decodeData); // decompressing
const orignalData = new TextDecoder().decode(de_compress_compressedData); // decoding the original value
console.log('+++++ encoded data 1', encodedDetails);
console.log('+++++ compressed data 2', compressedEncodedDetails);
console.log('++++++ decodeData data 3', decodeData);
console.log('++++++ encode_decodeData data 4', encode_decodeData);
console.log('++++++ d_compress_compressedData data 5', de_compress_compressedData);
console.log('++++++ d_compress_compressedData data 6', orignalData);
I don't really getting it where's the problem coming up, any hint or suggestion would be beneficial.
答案1
得分: 1
你正在尝试将一个压缩的二进制数据块解码成文本。这就是为什么在尝试解压缩时无法恢复原始数据。
在准备好解压缩之前,你可以考虑将数据保留为二进制(Uint8Array)。由于你试图将数据嵌入到QR码中,你可以考虑使用base64编码:
const encodedDetails = new TextEncoder().encode(JSON.stringify(this.studentDetails, null, 0)); // 编码成Uint8Array
const compressedEncodedDetails = await compress(encodedDetails); // 将Uint8Array压缩成Uint8Array
// 转换为Base64以用于QR码
const base64Encoded = btoa(String.fromCharCode.apply(null, compressedEncodedDetails));
// 将'base64Encoded'嵌入到你的QR码中
// 后来,当你读取QR码时,你可以将base64字符串解码回二进制以进行解压缩
const decodeData = atob(base64Encoded);
const byteArray = new Uint8Array(decodeData.length).map((_, i) => decodeData.charCodeAt(i));
const de_compress_compressedData = await decompress(byteArray); // 解压缩
const originalData = new TextDecoder().decode(de_compress_compressedData); // 解码原始值
console.log('++++++ original data', originalData);
希望这对你有帮助。
英文:
You are trying to decode a compressed binary blob as if it were a text. This is why it is not recoverable when trying to decompress it.
You could consider keeping the data as binary (Uint8Array) until you are ready to decompress it. Since you are trying to embed the data into a QR code, you could consider base64 encoding:
const encodedDetails = new TextEncoder().encode(JSON.stringify(this.studentDetails, null, 0)); // encoded in Uint8Array
const compressedEncodedDetails = await compress(encodedDetails); // compress Uint8Array into Uint8Array
// Convert to Base64 for QR code
const base64Encoded = btoa(String.fromCharCode.apply(null, compressedEncodedDetails));
// Embed 'base64Encoded' into your QR code
// Later, when you read the QR code, you can decode the base64 string back into binary for decompression
const decodeData = atob(base64Encoded);
const byteArray = new Uint8Array(decodeData.length).map((_, i) => decodeData.charCodeAt(i));
const de_compress_compressedData = await decompress(byteArray); // decompressing
const originalData = new TextDecoder().decode(de_compress_compressedData); // decoding the original value
console.log('++++++ original data', originalData);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论