From 8cf77c452f2b0197560f538c12649e8bce694faf Mon Sep 17 00:00:00 2001 From: Mingun Date: Mon, 18 Mar 2024 21:37:20 +0500 Subject: [PATCH] Python/Construct: Use `unsigned char` representation when converting to bytes using `.as` That means that converted array expected to be an array of integers in the range [0, 255]. Perl and Ruby also uses unsigned representation ('C*') --- .../scala/io/kaitai/struct/translators/TranslatorSpec.scala | 2 +- .../scala/io/kaitai/struct/translators/PythonTranslator.scala | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala index 9eaee3f4b..5ce2c97df 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -675,7 +675,7 @@ class TranslatorSpec extends AnyFunSpec { LuaCompiler -> "???", PerlCompiler -> "pack('C*', ((0 + 1), 5))", PHPCompiler -> "pack('C*', (0 + 1), 5)", - PythonCompiler -> "struct.pack('2b', (0 + 1), 5)", + PythonCompiler -> "struct.pack('2B', (0 + 1), 5)", RubyCompiler -> "[(0 + 1), 5].pack('C*')" )) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala index 57607aa81..9905b12d3 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/PythonTranslator.scala @@ -40,7 +40,8 @@ class PythonTranslator(provider: TypeProvider, importList: ImportList) extends B "b\"" + Utils.hexEscapeByteArray(arr) + "\"" override def doByteArrayNonLiteral(elts: Seq[Ast.expr]): String = { importList.add("import struct") - s"struct.pack('${elts.length}b', ${elts.map(translate).mkString(", ")})" + // Use `unsigned char` representation (see https://docs.python.org/3/library/struct.html#struct-format-strings) + s"struct.pack('${elts.length}B', ${elts.map(translate).mkString(", ")})" } override def doLocalName(s: String) = {