What is the equivalent Scala for the following Python statement? It looks like it’s using an unsigned long or something (which Scala doesn’t have).
size = pack('!L', len(someString))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s not an exact one-for-one mapping for this statement, but you can get close.
In Scala, you typically don’t store binary data in a string as
packdoes. Instead you store it in anArray[Byte].Scala itself doesn’t have any libraries for this, but Java does, and you can use them in your Scala code.
There are several things to note about why this works:
DataOutputStreamwrites in big-endian order, which matches your use of!in the Python code. If you were writing in little-endian order, you’d have to use a little-endian implementation of DataOutputStream.packmethod writes a long as 4 bytes, which matcheswriteIntinDataOutputStream.writeLongwould be the same aslong long(qorQ) inpack.