0

I want to have a string that can be converted into four numbers.

For example, it converts a string from E17B1237 into (225, 123, 18, 55), this is converted from hexadecimal into decimal.

E1 => 225,
7B => 123, 
12 => 18, 
37 => 55.

How can I do it? hexadecimal into decimal is just an example, is there any way to do that?

How do I distinguish a string E17B1237, split into (225, 123, 18, 55), then do the conversion. Thanks.

Because I want to have a rectangle in the coordinate, and use x, y, w, h to create a unique ID, then I can also use the unique ID to retrieve x, y, w, h.

How do I create a unique ID(from x,y,w,h) that can be clear to split into four numbers, then do the conversion.

leojail
  • 307
  • 3
  • 12
  • 2
    In your example, you are converting the first bit from hex to decimal, but the rest are converted from decimal to hex. How do you define what the desired output should be? First of the four parts to decimal, the rest to hex? – Timo Jan 16 '22 at 08:11
  • Sorry. I just edited my question. just convert from hex to decimal. Thanks. – leojail Jan 16 '22 at 08:13
  • 2
    Isn't what you're trying to do a little ambiguous? There is nothing in your string that separates the numbers. In your example, you've grabbed 3, then 3, then 2, then 2 numbers `225 | 123 | 18 | 55`. Is it always this pattern (3, 3, 2, 2)? For example, why isn't it grabbing 3, 3, 3, and then 1: `225 | 123 | 185 | 5`? Is it always supposed to grab the first 3 numbers, then the next 3, then the next 2 and then finally the last 2? – Nick Parsons Jan 16 '22 at 08:16
  • Because I want to have a rectangle in the coordinate, and use x, y, w, h to create a unique ID, then I can also use the unique ID to retrieve x, y, w, h. Thanks. – leojail Jan 16 '22 at 08:21
  • 1
    @leojail Still not too clear on how you're deciding to split/partition your string. Basically, I'm trying to understand how you're deciding which numbers to convert? Why is it `18` that you're converting to hex and not `185` for example? – Nick Parsons Jan 16 '22 at 08:23
  • This is one of my questions. How do I create a unique ID(from x,y,w,h) that can be clear to split into four numbers, then do the conversion. Really appreciate. – leojail Jan 16 '22 at 08:32
  • Where are these numbers coming from? Are you able to specify the format for this string? – Timo Jan 16 '22 at 08:52
  • Why exactly you need hex ID. If your coordinates are `var coords = [[12,41],[55,77]]` then just do like `coords.toString()` and you will have `'12,41,55,77'` unique string which you can use as a key in a map and strore `cords` as it's value. – Redu Jan 16 '22 at 08:52
  • The reason why I want to specify an ID, because I will use the ID to be tokenID for NFT in the smart contract, the ID spec should be uint256. – leojail Jan 16 '22 at 08:58
  • 2
    Then perhaps you should look into [`SubtleCrypto.digest()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) – Redu Jan 16 '22 at 09:09

2 Answers2

2

Assuming each hex code within your string has a length of 2, you can chunk your string into an array using .match(/[0-9A-F]{2}/ig) to match consecutive runs of two characters to get the following array:

['E1', '7B', '12', '37']

Once you have this array, you can use .map() on it to return a new array, where each hex code element within your array is converted into its equivalent base 10 (decimal) value. You can do this with the help of parseInt(string, radix), by passing 16 (the base for hexadecimal) as the radix to convert from:

const hexStr = 'E17B1237';
const res = hexStr.match(/[0-9A-F]{2}/ig).map(hexPart => parseInt(hexPart, 16));
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Really appreciate your answer. But hex code string maybe has 3 lengths, the length is not regular. How to do that? Thanks. – leojail Jan 16 '22 at 08:37
  • 1
    @leojail could you share the different lengths and how they might look and the expected output for those? – Nick Parsons Jan 16 '22 at 08:38
  • The coordinate range is 10000 x 10000. For example, Unique ID(hex) looks like 1901388C84B0, it corresponds to (x, y, w, h)(decimal) will be (400, 5000, 200, 1200). Thanks. – leojail Jan 16 '22 at 08:50
  • @leojail in that example there is more than one possible answer to the input. You've split your string like so `190 | 1388 | C8 | 4B0` and then converted each part to decimal, why isn't `1901 | 388 | C84 | B0` used instead for example? This means your input string is ambiguous and there isn't one way this can be decoded. Can you add some sort of delimiter, such as a comma, that you can split on, eg: `190,1388,C8,4B0`? Your encoded hex string either needs to be "fixed-length" or somehow made "prefix-free" if you want to distinguish them – Nick Parsons Jan 16 '22 at 09:01
  • Because finally, I will use the ID to be tokenID for NFT in the smart contract, the ID spec should be uint256. Thanks. – leojail Jan 16 '22 at 09:06
1

You can use toString paramters to convert

function convert(value, from, to) {
  return parseInt(value, from).toString(to);
}

console.log(
  convert('e1', 16, 10),
  convert('123', 10, 16),
);
samuelcolt
  • 253
  • 1
  • 5
  • 2
    Yeah, I think the partitioning of the string is an important part of the question that is not addressed in this answer. Otherwise, if this was just the answer then this question would be a duplicate of [How do you convert numbers between different bases in JavaScript?](https://stackoverflow.com/q/1337419) – Nick Parsons Jan 16 '22 at 07:56