在Scala中翻转字节字节序的最简单方法

时间:2016-01-14 20:59:12

标签: scala

标题说明了这一点,在Scala中翻转字节字节序的最简单方法是什么?

编辑:这是一个十六进制字符串的示例,可以转换为public ActionResult order(int? id) { // Logic to get the file path and file name from database // var ImageName = "file" // var filepath = will be something like ~/images/file.jpg byte[] filedata = System.IO.File.ReadAllBytes(filepath); string contentType = MimeMapping.GetMimeMapping(filepath); return File(filepath, contentType, ImageName+".jpg"); } 我正在谈论的内容

List[Byte]

1 个答案:

答案 0 :(得分:2)

如果你的意思是用Byte的高半位翻转低位:

(b: Byte) => (b >>> 4) + (b << 4)

如果您要在hex string上翻转每个半字节,我会grouped 2flip个字符,然后在创建string之前val input = "44[5e405f...".replaceAll("[^0-9a-f]","") val expected = "44[e504f5...".replaceAll("[^0-9a-f]","") // as suggested by @PaulDraper in the comments input.grouped(2).map(_.reverse).mkString == expected // or the verbose val ab = "(.)(.)".r input.grouped(2).map { case ab(a, b) => b ++ a }.mkString == expected List[Byte]再次:

val inputBytes = input.grouped(2).map(Integer.parseInt(_, 16).toByte)
val expectedBytes = expected.grouped(2).map(Integer.parseInt(_, 16).toByte)

inputBytes.map((b: Byte) => (b >>> 4) + (b << 4)).map(_.toByte).toSeq == expectedBytes.toSeq

如果您的输入是 RelativeLayout partieoption = (RelativeLayout) findViewById(R.id.partieoption); int wpartieoption =partieoption.getWidth(); int hpartieoption = partieoption.getHeight(); ImageView x = (ImageView) findViewById(R.id.photo2); x.setX( (float) ( hpartieoption/2 ) );

                    android:id="@+id/partieoption"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:weightSum="6"
                    android:orientation="horizontal"
                    >




                        <ImageView 
                            android:id="@+id/photo2"
                            android:src="@drawable/loglog"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            />




               </RelativeLayout>