Convert decimal integer to hex in Elixir - hex

I know we can declare an integer in base 2, 8, 10, or 16, for example:
0b10000
0o20
16
0x10
all result in the integer 16.
But given an integer, for example 43981, how do I get its hexadecimal representation?

Use Integer.to_string/2 with 16 as the second argument.
Integer.to_string(43981, 16) # "ABCD"
You can also get the binary and octal representations the same way:
Integer.to_string(43981, 2) # "1010101111001101"
Integer.to_string(43981, 8) # "125715"

Related

What is the replacement for dec() function in julia?

dec() => a String representation showing that what the decimal version would be.
julia> dec(0x10)
ERROR: UndefVarError: dec not defined
Stacktrace:
[1] top-level scope at REPL[1]:1
You can use the string function:
string(n::Integer; base::Integer = 10, pad::Integer = 1)
Convert an integer n to a string in the given base, optionally
specifying a number of digits to pad to.
julia> string(5, base = 13, pad = 4) "0005"
julia> string(13, base = 5, pad = 4) "0023"
Since base defaults to 10 in your case you can do:
julia> string(0x10)
"16"

Need help understanding how gsub and tonumber are used to encode lua source code?

I'm new to LUA but figured out that gsub is a global substitution function and tonumber is a converter function. What I don't understand is how the two functions are used together to produce an encoded string.
I've already tried reading parts of PIL (Programming in Lua) and the reference manual but still, am a bit confused.
local L0_0, L1_1
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
encodes = L0_0
L0_0 = gg
L0_0 = L0_0.toast
L1_1 = "__loading__\226\128\166"
L0_0(L1_1)
L0_0 = encodes
L1_1 = --"The Encoded String"
L0_0 = L0_0(L1_1)
L1_1 = load
L1_1 = L1_1(L0_0)
pcall(L1_1)
I removed the encoded string where I put the comment because of how long it was. If needed I can upload the encoded string as well.
gsub is being used to get 2 digit sections of A0_2. This means the string A0_3 is a 2 digit hexadecimal number but it is not in a number format so we cannot preform math on the value. A0_3 being a hex number can be inferred based on how tonubmer is used.
tonumber from Lua 5.1 Reference Manual:
Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.
An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. In base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see ยง2.1). In other bases, only unsigned integers are accepted.
So tonumber(A0_3, 16) means we are expecting for A0_3 to be a base 16 number (hexadecimal).
Once we have the number value of A0_3 we do some math and finally convert it to a character.
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
This block of code takes a string of hex digits and converts them into chars. tonumber is being used to allow for the manipulation of the values.
Here is an example of how this works with Hello World:
local str = "Hello World"
local hex_str = ''
for i = 1, #str do
hex_string = hex_string .. string.format("%x", str:byte(i,i))
end
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
local encoded = L0_0(hex_str)
print(encoded)
Output
;X__bJbe_W
And taking it back to the orginal string:
function decode(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 13) % 256)
end))
end
hex_string = ''
for i = 1, #encoded do
hex_string = hex_string .. string.format("%x", encoded:byte(i,i))
end
print(decode(hex_string))

How to convert a group of Hexadecimal to Decimal (Visual Studio )

I want to retrieve like in Pic2, the values in Decimal. ( hardcoded for visual understanding)
This is the codes to convert Hex to Dec for 16 bit:
string H;
int D;
H = txtHex.Text;
D = Convert.ToInt16(H, 16);
txtDec.Text = Convert.ToString(D);
however it doesn't work for a whole group
So the hex you are looking at does not refer to a decimal number. If it did refer to a single number that number would be far too large to store in any integral type. It might actually be too large to store in floating point types.
That hex you are looking at represents the binary data of a file. Each set of two characters represents one byte (because 16^2 = 2^8).
Take each pair of hex characters and convert it to a value between 0 and 255. You can accomplish this easily by converting each character to its numerical value. In case you don't have a complete understanding of what hex is, here's a map.
'0' = 0
'1' = 1
'2' = 2
'3' = 3
'4' = 4
'5' = 5
'6' = 6
'7' = 7
'8' = 8
'9' = 9
'A' = 10
'B' = 11
'C' = 12
'D' = 13
'E' = 14
'F' = 15
If the character on the left evaluates to n and the character on the right evaluates to m then the decimal value of the hex pair is (n x 16) + m.
You can use this method to get your values between 0 and 255. You then need to store each value in an unsigned char (this is a C/C++/ObjC term - I have no idea what the C# or VBA equivalent is, sorry). You then concatenate these unsigned char's to create the binary of the file. It is very important that you use an 8 bit type to store these values. You should not store these values in 16 bit integers, as you do above, or you will get corrupted data.
I don't know what you're meant to output in your program but this is how you get the data. If you provide a little more information I can probably help you use this binary.
You will need to split the contents into separate hex-number pairs ("B9", "D1" and so on). Then you can convert each into their "byte" value and add it to a result list.
Something like this, although you may need to adjust the "Split" (now it uses single spaces, returns, newlines and tabs as separator):
var byteList = new List<byte>();
foreach(var bytestring in txtHex.Text.Split(new[] {' ', '\r', '\n', '\t'},
StringSplitOptions.RemoveEmptyEntries))
{
byteList.Add(Convert.ToByte(bytestring, 16));
}
byte[] bytes = byteList.ToArray(); // further processing usually needs a byte-array instead of a List<byte>
What you then do with those "bytes" is up to you.

How to store the ASCII value in a character (Ada 83 Only)

How do I store the ascii value of an integer (say 33) in a character. I want something like this in Ada83, not 95
C: Code
char c = 10;
char *k = &c;
strncat (des, k, 1);
printf("%s",des);
Thank you!!
C : Character := Character'Val(10);
or
C : Character := ASCII.LF;
The first works in all versions of Ada. The second one was the standard way in Ada 83; it is now obsolescent. The newer way is
C : Character := Ada.Characters.Latin_1.LF;
More information: In Ada, Character is an enumeration type, not an integer type. Therefore, you can't assign an integer to it directly. The 'Val attribute is the Ada way to convert an integer to an enumeration; Enum_Type'Val(N) means "the Nth enumeration literal defined for the enumeration type, 0-relative". To go the other way, Enum_Type'Pos(E) returns the integer corresponding to the position of E in the enumeration list.

How to produce Capital hexadecimal digits with QString::arg() ? [QT]

I'm trying to create a QString which is a hexadecimal number with its letter digits in Capitals instead of small caps, how can it be done?
QString( " %1" ).arg( 15, 1, 16 )
yields f and I'd like F
By converting the string to upper case:
QString( " %1" ).arg( 15, 1, 16 ).toUpper();
This returns an uppercase string. The method used to be called upper() in qt3.

Resources