Hexadecimal string to decimal conversion

I have 2 pyspark columns consisting of following hexadecimal values:

Value            | 245FC;324EE;
Value_Split      | [245FC,324EE] 

I would like to convert them to the following decimal numbers:

Value            | 148988;206062;
Value_Split      | [148988,206062]

I am happy, even one column conversion happens.

Upvotes: 0

Views: 311

Answers (1)

Lamanus
Lamanus

Reputation: 13551

Use conv function to change hexadecimal to the decimal.

Spark >= 3.1.0

df.withColumn('Value_Split', f.transform('Value_Split', lambda v: f.conv(v, 16, 10))) \
  .show(truncate=False)

+----------------+
|Value_Split     |
+----------------+
|[148988, 206062]|
+----------------+

Spark >= 2.4.0

df.withColumn('Value_Split', f.expr('transform(Value_Split, v -> conv(v, 16, 10))')) \
  .show(truncate=False)

+----------------+
|Value_Split     |
+----------------+
|[148988, 206062]|
+----------------+

Upvotes: 1

Related Questions