---
title: "How to convert int to hex in java?"  
description: "How to convert int to hex in java?"  
author: "Anonymous User"  
published: 2014-10-16  
updated: 2014-10-16  
canonical: https://www.mindstick.com/forum/2388/how-to-convert-int-to-hex-in-java  
category: "java"  
tags: ["java", "oops"]  
reading_time: 1 minute  

---

# How to convert int to hex in java?

I have the following code.

```
int Val = -32768;String Hex = Integer.toHexString(Val);
```

This equates to ffff8000.\

```
int FirstAttempt = Integer.parseInt(Hex, 16); // Error "Invalid Int"int SecondAttempt = Integer.decode("0x" + Hex);  // Error "Invalid Int"
```

So, initially, it converts the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) -32768 into a hex [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) ffff8000, but then it can't [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) the hex string back into an Integer.

In.Net it works as I'd expect, and returns -32768.

I know that I could write my own little [method](https://www.mindstick.com/forum/166/webservice-method) to convert this myself, but I'm just wondering if I'm [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) something, or if this is genuinely a bug?

## Replies

### Reply by Anonymous User

It overflows, because the number is negative.

Try this and it will work:

int n = (int)Long.parseLong("ffff8000", 16);


---

Original Source: https://www.mindstick.com/forum/2388/how-to-convert-int-to-hex-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
