-3

There are pre-translated numbers from hexadecimal to decimal. For example, 0x01, 0x10 and so on. I need to extract from it everything that is after x, so that I get a number that could fit as an index to the array.

The difficulty lies in the fact that the hex value is an Integer (NOT AS a String), that is, it is in this format:

int hexValue = 16; // 0x10 

And I need to get the answer 10.

Why is this necessary? I need the hexadecimal value to be a full-fledged index for the array

  • 1
    `String.valueOf(n)` Um, if `n` is your input, it already is "decimal." Just use it. – markspace Jul 19 '23 at 21:06
  • those 2 things don't do the same thing. In fact, I'm not sure your second function does anything at all? – njzk2 Jul 19 '23 at 21:08
  • @markspace changed the post – user22150393 Jul 19 '23 at 21:09
  • It's still decimal. The `0x` part does the conversion for you. In other words if you print that value it'll print "16". – markspace Jul 19 '23 at 21:10
  • @markspace I know, but I don't specify the number in advance, I don't know it – user22150393 Jul 19 '23 at 21:11
  • 2
    You're not making any sense. I think you'll most likely need to clarify what your input is, but if it's a "number" then it's just a number and decimal or hex doesn't matter until you choose to print it. Numbers internally are all the same. – markspace Jul 19 '23 at 21:12
  • 7
    Hexadecimal and decimal as well as binary are just different representations of the same number. You cannot convert from an *hexadecimal int* to a *decimal int* because there is no such thing as a *hexadecimal int* or *decimal int*. There is only one `int` datatype which will be stored in binary, the rest is just how it's represented as a String. – Mushroomator Jul 19 '23 at 21:13
  • 2
    Here is a 100% correct line of code that will take `int hex = 0x10` and properly give you the value of that in hexadecimal. This line of code is _absolutely correct_ and will be correct for all inputs. Here it is: `int decimal = hex;` I can even guarantee that it'll be extremely efficient, and a proper benchmark will show you that it takes zero nanoseconds. – Louis Wasserman Jul 19 '23 at 21:20
  • Have you determined that Integer.parseInt(x, 16) is actually using "too many resources" for your intended purpose (and identified what said "resources" actually are...?). – Neil Coffey Jul 19 '23 at 21:22
  • @NeilCoffey In my case, this will be a load on the garbage collector, since the conversion to String requires the creation of an array – user22150393 Jul 19 '23 at 21:24
  • "convert from hexadecimal to decimal java" makes sense if your input is in character format, and you want the output to be in character format. But, your example code doesn't show or otherwise indicate that is the case. Note I deleted both occurrences of "integer" in the quote. – Old Dog Programmer Jul 19 '23 at 21:25
  • @OldDogProgrammer This is the point that the Integer value is in the hexadecimal system in advance, and I need to convert to the decimal system – user22150393 Jul 19 '23 at 21:30
  • If we are confused, it is because you are not telling us enough about what it is you are trying to do, or what problem you are actually trying to solve. – Old Dog Programmer Jul 19 '23 at 21:32
  • With your update, how would it convert a hexValue of 10? – Tim Moore Jul 19 '23 at 21:37
  • `int hex = 0x10;`, `int hex = 020;`, `int hex = 0b10000`, and `int hex = 16;` all do *exactly* the same thing. – Old Dog Programmer Jul 19 '23 at 21:50
  • 1
    I guess they want to convert `0x10` to `10`, `0x16` to `16`, etc. for some reason, but it’s unclear how to handle hexadecimal digits greater than 9. – Tim Moore Jul 19 '23 at 21:54
  • @TimMoore It's true. I'm already confused myself, but that's what I wanted – user22150393 Jul 19 '23 at 21:57
  • 1
    Suggested reading: [How to Ask a Good Queston](https://stackoverflow.com/help/how-to-ask) and [Complete, Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) . The example you have given is sub-minimal and incomplete. – Old Dog Programmer Jul 19 '23 at 22:05
  • 1
    I don’t know what “ a full-fledged index for the array” means. What array? Why are its indices specified in this way? What should happen if the number provided can’t be converted this way? – Tim Moore Jul 19 '23 at 22:26
  • 3
    I suspect this is an [XY Problem](https://en.wikipedia.org/wiki/XY_problem). The explanations need to be expanded. The examples need to be expanded. We need more explanation of what is trying to be done. Where do the numbers come from? If they are from a file, show code that reads the file. Show an example of the contents of the file. Show code for the array that needs to be indexed. – Old Dog Programmer Jul 19 '23 at 22:30
  • 1
    When you say you want to get the value '10' from `int hex = 16`, what value do you mean? Do you want the value we call 'ten' or the value we call 'sixteen', both of which can represented by the sequence of characters 'digit one followed by digit zero'. As others have said, you are confusing the actual number, which is fixed at sixteen, with some sequence of characters, which sequence depends on how you decide to convert the number to characters. – Arfur Narf Jul 20 '23 at 03:10
  • Why have you removed your code ? – Rohit Gupta Jul 25 '23 at 05:19
  • 1
    Do you still want help with this? If so, you need to edit the question again. Go back, start at the beginning. Tell us where these numbers come from. Explain the problem you have with indexing the array with these numbers. – Old Dog Programmer Jul 25 '23 at 16:47
  • 1
    Your revision does not explain why you think you need the number `10` to index your array, or even what you think the number `10` is. Please show much more of your code: where the number comes from, and what you do with it. – tgdavies Jul 26 '23 at 06:03

1 Answers1

1

Hex, binary, octal, etc are all visual presentations of a number in a specific base. Any int is already in "binary." It could be +5 (1) and ground (0) if you want to go that deep into it.

So converting an int to another base does not make sense unless it is for display purposes.

Or given a string in a particular base and converting that to an int so it can be used in mathematical expressions or data structures is also an option.

Per your comment I still believe you are somewhat confused about numeric representation. But here is how to convert a hex string to an int. This does not handle signed values.

String hexString = "2A4";
 
String hexDigits = "0123456789ABCDEF";
int result = 0;
for (char c : hexString.toCharArray()) {
     result = result * 16 + hexDigits.indexOf(c);
}
System.out.println(result);

prints

676

And let me repeat that it does not make sense to try and convert any int from one base to an int in another base, as int's are not really in any base at all. ints are treated as decimal by the compiler and we talk about them as though they are binary because it provides a common and accepted way to describe them.

Updated

You may want to check out Integer.decode. If you are reading in encoded strings with prefixes of x,X, and # for hex ,0 for octal, and a non-zero digit for decimal, you can do the following.

String [] codes = {"0x11", "021", "#11", "17"};
 
for (String code : codes) {
     System.out.println(Integer.decode(code));
}

prints

17
17
17
17
 
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I want to convert a hexadecimal integer to decimal to use it as an index to an array – user22150393 Jul 19 '23 at 21:15
  • 3
    There is no such thing as a "hexadecimal integer". It's all just integers. Just try using the number without conversion, tell us the result. It'll probably be correct. – markspace Jul 19 '23 at 21:16
  • 3
    As I said, there is no such thing as hexadecimal integer. It is just an integer and is best described as a binary value. However, math operations treat it as decimal by default. – WJS Jul 19 '23 at 21:16
  • I need to convert a hexadecimal number to decimal like in the first example, but without using the String conversion. I know in advance that it is in the hexadecimal system, but I do not know the original number in decimal – user22150393 Jul 19 '23 at 21:21
  • 1
    @user22150393 "_I know in advance that it is in the hexadecimal system_" – What does this mean? Where are you getting this number? If it's already an `int`, then **there's no conversion necessary**. In fact, in that context, converting the base has no meaning. If you have e.g., `int d = 255;` (decimal), `int h = 0xFF;` (hex), `int o = 0377;` (octal), and `int b = 0b11111111;` (binary) then all four are the **same number** already. You could substitute any of the four variables with another in a mathematical expression and get the same answer. – Slaw Jul 19 '23 at 21:53