How to convert Hex string to Hex value in scala? - scala

I am new to scala and was trying out a few basic concepts. I have an Integer value that I am trying to convert an integer x into a hex value using the following command
val y = Integer.toHexString(x)
This value gives me the hex number in a string format. However I want to get the hex value as a value and not a string. I could write some code for it, but I was wondering if there was some direct command available to do this? Any help is appreciated.
Edit: For example with an integer value of say x =38
val y = Integer.toHexString(38)
y is "26" which is a string. I want to use the hex value 0x26 (not the string) to do bitwise AND operations.

Hex is simply a presentation of a numerical value in base 16. You don't need a numeric value in hexadecimal representation to do bitwise operations on it. In memory, a 32bit integer will be stored in binary format, which is a different way of representation that same number, only in a different base. For example, if you have the number 4 (0100 in binary representation, 0x4 in hex) as variable in scala, you can bitwise on it using the & operator:
scala> val y = 4
y: Int = 4
scala> y & 6
res0: Int = 4
scala> y & 2
res1: Int = 0
scala> y & 0x4
res5: Int = 4
Same goes for bitwise OR (|) operations:
scala> y | 2
res2: Int = 6
scala> y | 4
res3: Int = 4

You do not need to convert the integer to a "hex value" to do bitwise operations. You can just do:
val x = 38
val y = 64
x | y
In fact, there is no such thing as a "hex value" in memory. Every integer is stored in binary. If you want to write an integer literal in hex, you can prefix it with 0x:
val x = 0x38 // 56 in decimal.
x | 0x10 // Turn on 5th bit.

Related

How to avoid round off and scientific notation in double in scala?

Given:
scala> val s =99999999999999947.89
s: Double = 9.9999999999999952E16
scala> f"$s%.2f"
res15: String = 99999999999999952.00
i want output should be = 99999999999999947.89
how to avoid round off and scientific notation both.
The Double datatype doesn't support the precision you're after. The closest number 99999999999999952 is the number closest to 99999999999999947.89 that can be represented as a Double.
That's not just the case for very big numbers. Double values can soon start to drift. You can see this for example in that (0.2 + 0.2 + 0.2) != 0.6
Use a BigDecimal instead.

scala can't make an add on a long

I'm not able to do an add on long type.
scala or the processor doesn't manage correctly the sign
scala> var i="-1014570924054025346".toLong
i: Long = -1014570924054025346
scala> i=i+92233720368547758L
i: Long = -922337203685477588
scala> var i=9223372036854775807L
i: Long = 9223372036854775807
scala> i=i+5
i: Long = -9223372036854775804
The first test where a negative number doesn't pass to a positive one is a problem for me
I have not fully understood the question, but for the first example, you get the expected result. What happens in the second example, the Long number happens to be the maximum value for a Long (i.e Long.MaxValue) so essentially when you had another positive number, it's overflowing:
scala> Long.MaxValue
res4: Long = 9223372036854775807L
scala> Long.MaxValue + 1
res7: Long = -9223372036854775808L // which is Long.MinValue
scala> Long.MinValue + 4
res8: Long = -9223372036854775804L // which is the result that you get
In other words:
9223372036854775807L + 5
is equivalent to:
Long.MaxValue + 5
which is equivalent to:
Long.MinValue + 4 // because (Long.MaxValue + 1) = Long.MinValue
which is equals to -9223372036854775804L
If you really need to use such big numbers, you might try using BigInt
scala> val x = BigInt(Long.MaxValue)
x: scala.math.BigInt = 9223372036854775807
scala> x + 1
res6: scala.math.BigInt = 9223372036854775808
scala> x + 5
res11: scala.math.BigInt = 9223372036854775812
scala> x + 10
res8: scala.math.BigInt = 9223372036854775817
scala> x * 1000
res10: scala.math.BigInt = 9223372036854775807000
scala> x * x
res9: scala.math.BigInt = 85070591730234615847396907784232501249
scala> x * x * x * x
res13: scala.math.BigInt = 7237005577332262210834635695349653859421902880380109739573089701262786560001
scala>
The documentation on BigInt is rather, err, small. However, i believe that it is basically an infinite precision integer (can support as many digits as you need). Having said that, there will probably at some point be a limit. There is a comment on BigDecimal - which has more documentation - that at about 4,934 digits there might be some deviation between BigDecimal and BigInt.
I will leave it to someone else to work out whether or not x ^ 4 is the value shown above.
Oh, I almost forgot your negative number test, I aligned the sum with the initialisation, to make it easier to visualise that the result appears to be correct:
scala> val x = BigInt("-1014570924054025346")
x: scala.math.BigInt = -1014570924054025346
scala> x + 92233720368547758L
res15: scala.math.BigInt = -922337203685477588
scala>
As for Ints, Longs and similar data types, they are limited in their size due to the number of bits they are constrained to. Int's are typically 32 bit and longs are typically 64 bits.
It is easier to visualise when you look at them in hexadecimal. A signed Byte (at 8 bits) has a maximum positive value of 0x7F (127). When you add one to it, you get 0x80 (-128). This is because we use the "Most Significant Bit" as an indicator of whether the number is positive or negative.
If the same byte was interpreted as unsigned, then 0x7F (127) would still become 0x80 when 1 is added to it. However, since we are interpreting it as unsigned, this would be equivalent to 128. We can keep adding one until we get to 0xFF (255) at which point if we add another 1 we will end up at 0x00 again which is of course 0.
Here are some references that explain this in much more detail:
Wikipedia - Twos complement
Cornell University - what is twos complement
Stack Overflow - what is 2s complement

Why spark map produces different result as RDD when input element converted to int/string in spark

I'm just trying to create a pairRDD using map and my input file contains 3 lines of numbers like below,
12
34
36
and having it in val lines.
Below line of code,
val pairRDD=lines.map(x => (x(0).toInt,x(1).toInt))
produces
(49,50)
(51,52)
(51,54)
as result
&
val pairRDD=lines.map(x => (x(0),x(1)))
produces
(1,2)
(3,4)
(3,6)
as result.
so, the only difference between 2 lines of code is, I'm converting to Int before mapping it but, however, I have just numbers in input files unchanged.
could someone help me to understand?
The difference comes from how scala Char.toInt works, firstly each line is a string, and indexing a string, you get a character, the Char.toInt method gets the ASCII value of that character, so '1' is 49, for instance:
val x = "12"
// x: String = 12
x(0)
// res12: Char = 1
x(0).toInt
// res13: Int = 49
To convert the digit literally to an Int, you can convert it to String, then to Int:
x(0).toString.toInt
// res15: Int = 1

long int or double in python

How can I convert m and n to long int or double in Python 3.4? Currently for large integers (ex:65535), the output is 'None'?
m = eval(input("Enter value for m: "))
n = eval(input("Enter value for n: "))
>>> float("1234.56")
1234.56
>>> int("1234")
1234
>>> long("12345678910")
12345678910
The float() and int() operators will be sufficient, as Python floats are usually the equivalent of C doubles, and if the integer is too large, int() will cast it to a long int. However, the long() operator will work if you want to explicitly cast to a long.
Python Numeric Types Docs for more info.
The eval() function is not a type casting operator, but rather evaluates strings or code objects as Python code, which can lead to problems if you're not careful:
>>> eval("1+2")
2
input() returns a string, which when evaluated returns None. If you're trying to assign an integer value to your variables, use the int() function:
m = int(input("Enter value for m: "))
n = int(input("Enter value for n: "))
Also, using eval is almost always a bad idea, as it runs the input as Python code, with the permissions of the script. If you must translate input, consider using ast.literal_eval instead.

Why converting '1' char to int using toInt method results to 49?

I want to convert a char to an int value.
I am a bit puzzled by the way toInt works.
println(("123").toList) //List(1, 2, 3)
("123").toList.head // res0: Char = 1
("123").toList.head.toInt // res1: Int = 49 WTF??????
49 pops up randomly for no reason.
How do you convert a char to int the right way?
For simple digit to int conversions there is asDigit:
scala> "123" map (_.asDigit)
res5: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3)
Use Integer.parseInt("1", 10). Note that the 10 here is the radix.
val x = "1234"
val y = x.slice(0,1)
val z = Integer.parseInt(y)
val z2 = y.toInt //equivalent to the line above, see #Rogach answer
val z3 = Integer.parseInt(y, 8) //This would give you the representation in base 8 (radix of 8)
49 does not pop up randomly. It's the ascii representation of "1". See http://www.asciitable.com/
.toInt will give you the ascii value. It's probably easiest to write
"123".head - '0'
If you want to handle non-numeric characters, you can do
c match {
case c if '0' <= c && c <= '9' => Some(c - '0')
case _ => None
}
You can also use
"123".head.toString.toInt