Converting a hexadecimal string to a decimal number - type-conversion

In Ruby one can convert a hexadecimal string to a decimal number with String#hex method:
"1a2f".hex # => 6703
How to do that in Crystal?

Just use String#to_i with a base of 16:
"1a2f".to_i(16) # => 6703

Related

Converting a hex to string in Swift formatted to keep the same number of digits

I'm trying to create a string from hex values in an array, but whenever a hex in the array starts with a zero it disappears in the resulting string as well.
I use String(value:radix:uppercase) to create the string.
An example:
Here's an array: [0x13245678, 0x12345678, 0x12345678, 0x12345678].
Which gives me the string: 12345678123456781234567812345678 (32 characters)
But the following array: [0x02345678, 0x12345678, 0x02345678, 0x12345678] (notice that I replaced two 1's with zeroes).
Gives me the string: 234567812345678234567812345678 (30 characters)
I'm not sure why it removes the zeroes. I know the value is correct; how can I format it to keep the zero if it was there?
The number 0x01234567 is really just 0x1234567. Leading zeros in number literals don't mean anything (unless you are using the leading 0 for octal number literals).
Instead of using String(value:radix:uppercase), use String(format:).
let num = 0x1234567
let str = String(format: "%08X", num)
Explanation of the format:
The 0 means to pad the left end of the string with zeros as needed.
The 8 means you want the result to be 8 characters long
The X means you want the number converted to uppercase hex. Use x if you want lowercase hex.

How to convert from Octal to Decimal in Swift

Hi I am trying to convert an octal number to decimal in swift. What would be the easiest way to do this?
From Octal to Decimal
There is a specific Int initializer for this
let octal = 10
if let decimal = Int(String(octal), radix: 8) {
print(decimal) // 8
}
From Decimal to Octal
let decimal = 8
if let octal = Int(String(decimal, radix: 8)) {
print(octal) // 10
}
Note 1: Please pay attention: parenthesis are different in the 2 code snippets.
Note 2: Int initializer can fail for string representations of number with more exotic radixes. Please read the comment by #AMomchilov below.
you can convert from octal to decimal easily. Swift supports octal syntax natively. You have to write "0o" before the octal number.
let number = 0o10
print(number) // it prints the number 8 in decimal
Integer Literals
Integer literals represent integer values of unspecified precision. By
default, integer literals are expressed in decimal; you can specify an
alternate base using a prefix. Binary literals begin with 0b, octal
literals begin with 0o, and hexadecimal literals begin with 0x.
Here is the documentation's reference.
I hope it helps you

convert formatted mac address to an hex value

I have a mac address as a string in the following format: xx:xx:xx:xx:xx:xx (six groups of two hexadecimal digits, separated by colons).
I want to convert the string into it's respective hex value (as a System.Byte type).
How can I convert it?
this?
"00:0a:fe:25:af:db" -split ':' | % { [byte]"0x$_" }
Edit after comment:
this?
[UInt64] "0x$("00:0a:fe:25:af:db" -replace ':')"

Format String to truncate a number to a specific number of digits

Is there a format string to truncate a number to a specific number of digits?
For example, any number greater than 5 digits i would like to truncate to 3 digits.
132456 -> 132
5000000 -> 500
#Erik : Format specifiers like %2d are specific to a language? I actually want to use it in javascript
Pseudo-Code
Function returning a String, receiving a String representing a Number as a parameter
IF the String has more than 5 characters
RETURN a substring containing the first 3 characters.
ELSE
RETURN the string received as a parameter
END IF
END Function
I assume you refer to printf format strings. I couldn't find anything that will truncate an integer argument (i.e. %d). But you can specify the maximum length of a string by referring to a string format string and specifying lengths via "%<MinLength>.<MaxLength>s".
So in your case you could turn your number arguments into strings and then use "%3.3s".

How to convert 64 bits integer represented as decimal string into hex string

How to convert 64 bits integer represented as decimal string into hex string?
I need to do it in Perl on system that doesn't support Quads.
use Math::BigInt;
my $decimal_string = '123456789123456789';
$hex_string = Math::BigInt->new($decimal_string)->as_hex();
use bignum;