Input HEX as a string - powershell

How can I treat a string which is a hexadecimal number as a hexadecimal number? For example, I am loading in a file of hexadecimal numbers but Python is loading the file in as a string. Is it possible to get Powershell to treat the hexadecimal numbers in the file as hexadecimal numbers?
Background: I'm asking the user for a Mac Address and I need to add x numbers to the last block to get the next x Mac addresses. I can split on ":" and get the last block but this is a string containing for example "13", which is not 13 decimal but 13 hex.

Try converting it first to an int by casting the variable with the base 16 as the second argument
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
then just add 1 to the result and use
hex(hex_int)
to get the result back to hex format

Just the whole MAC address in once by using the PhysicalAddress.Parse(String) Method:
$MAC = 'F0:E1:D2:C3:B4:A5' -Replace '[^0-9a-fA-F]' # remove all the non-Hex characters
[System.Net.NetworkInformation.PhysicalAddress]::Parse($MAC).GetAddressBytes()
240
225
210
195
180
165

Solved it by doing it like this:
$MacNr = ([int64]"0x$($MacAddress.Split(":")[5])")
$MacNr ++1
$NewMac = ('{0:X2}' -f $MacNr)

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.

Convert text to binary and store in a single array in matlab

I need to convert the given text (not in file format) into binary values and store in a single array that is to be given as input to other function in Matlab .
Example:
Hi how are you ?
It is to be converted into binary and stored in an array.I have used dec2bin() function but i did not suceed in getting the output required.
Sounds a bit like a trick question. In MATLAB, a character array (string) is just a different representation of 16-bit unsigned character codes.
>> str = 'Hi, how are you?'
str =
Hi, how are you?
>> whos str
Name Size Bytes Class Attributes
str 1x16 32 char
Note that the 16 characters occupy 32 bytes, or 2 bytes (16-bits) per character. From the documentation for char:
Valid codes range from 0 to 65535, where codes 0 through 127 correspond to 7-bit ASCII characters. The characters that MATLABĀ® can process (other than 7-bit ASCII characters) depend upon your current locale setting. To convert characters into a numeric array,use the double function.
Now, you could use double as it recommends to get the character codes into double arrays, but a minimal representation would simply involve uint16:
int16bStr = uint16(str)
To split this into bytes, typecast into 8-bit integers:
typecast(int16bStr,'uint8')
which yields 32 uint8 values (bytes), which are suitable for conversion to binary representation with dec2bin, if you want to see the binary (but these arrays are already binary data).
If you don't expect anything other than ASCII characters, just throw out the extra bits from the start:
>> int8bStr =
72 105 44 32 104 111 119 32 97 114 101 32 121 111 117 63
>> binStr = reshape(dec2bin(binStr8b.'),1,[])
ans =
110011101110111001111111111111110000001001001011111011000000 <...snip...>

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 ':')"

XOR, MD5 and Base64 encoding issue

i need to get value which first 16 characters are TZxy2o2h2I2NMVR+ for which I have a formula. The formula goes like this: Base64(XOR("KonstantaZaLDAP", MD5(521009)) + XOR(521009, "KonstantaZaLDAP")) or in a word:
I have two values:
int radID = 521009
String konst = "KonstantaZaLDAP"
The first step is to apply XOR operation to konst and MD5 hash value of konst >>XOR(kost, MD5(radID))
Second, I need to apply XOR operation to radID and konst >> XOR(radID, konst).
After that i should concatenate values from first and second step >> XOR(kost, MD5(radID)) + XOR(radID, konst) and finaly Base64 encode concatenated value.
That is Base64(XOR(konst, MD5(radID)) + XOR(radID, konst)).
I have tried to achieve wanted value, and whatever I do, I get first 13 characters right, and after that it's all wrong. The value I get is TZxy2o2h2l2NMfUfpPmJNA==
Can anyone help!?

Convert numbers 1-26 to A-Z?

How can I convert the numbers in the range 1 through 26 to their respective letter position in the alphabet?
1 = A
2 = B
...
26 = Z
CHR(#) will give you the ASCII character, you just need to offset it based on the ASCII table:
e.g. A = 65, so you will need to add 64 to 1:
CHR(64 + #) = A if # is 1
ASCII code is the numerical representation of a character such as 'a' or 'Z'. Therefore by looking at the table one can see that capital A has a value of 65 and Z has a value of 90. Adding 64 from each value in the range 1-26 will give you their corresponding letter.