Given a list of decimal numbers, how can each number be converted to its equivalent hexadecimal value, and vice versa?
For example:
(convert2hex 255 64 64); -> (FF 40 40)
(convert2dec FF 40 40); -> (255 64 64)
(convert2hex 255 64 64 255 64 64 128)
(convert2dec FF 40 40 FF 40 40 80)
Number to Hex:
(format "%X" 255) ;; => "FF"
You can also zero-pad the value with:
(format "%03X" 255) ;; => "0FF"
Where the 0 is the character to use for padding and 3 is the number of spaces to pad.
Hex string to number
(string-to-number "FF" 16) ;; => 255
The 16 means "read as base-16."
If you just want to type a hexadecimal number into Emacs, there's no need to call string-to-number, just use the #x reader syntax:
#xFF
==> 255
You can also use #b for binary, #o for octal numbers, or #36r for base 36:
#b10011001
==> 153
#o777
==> 511
#36rHELLO
==> 29234652
See section 3.1 Integer Basics in the Emacs Lisp Manual
I was interested in converting octal chars to hex chars in a source file.
I wanted to query search and replace on the following text:
'\0' -> "\\0", // Null
'\7' -> "\\a", // Bell (^G)
'\b' -> "\\b", // Backspace (^H)
'\13' -> "\\v", // Vertical tab (^K)
'\33' -> "\\e", // Escape (^[)
Here was the regular expression search and replace
C-M-%
'\\\([0-9]+\)'
RET
'\,(format "\\0x%04X" (string-to-number \1 8))'
RET
The result is:
'\0x0000' -> "\\0", // Null
'\0x0007' -> "\\a", // Bell (^G)
'\b' -> "\\b", // Backspace (^H)
'\0x000B' -> "\\v", // Vertical tab (^K)
'\0x001B' -> "\\e", // Escape (^[)
Related
A script I am making scans a 5-character code and assigns it a number based on the contents of characters within the code. The code is a randomly-generated number/letter combination. For example 7D3B5 or HH42B where any position can be any one of (26 + 10) characters.
Now, the issue I am having is I would like to figure out the number from 1-(36^5) based on the code. For example:
00000 = 0
00001 = 1
00002 = 2
0000A = 10
0000B = 11
0000Z = 36
00010 = 37
00011 = 38
So on and so forth until the final possible code which is:
ZZZZZ = 60466176 (36^5)
What I need to work out is a formula to figure out, let's say G47DU in its number form, using the examples below.
Something like this?
function getCount(s){
if (!isNaN(s))
return Number(s);
return s.charCodeAt(0) - 55;
}
function f(str){
let result = 0;
for (let i=0; i<str.length; i++)
result += Math.pow(36, str.length - i - 1) * getCount(str[i]);
return result;
}
var strs = [
'00000',
'00001',
'00002',
'0000A',
'0000B',
'0000Z',
'00010',
'00011',
'ZZZZZ'
];
for (str of strs)
console.log(str, f(str));
You are trying to create a base 36 numeric system. Since there are 5 'digits' each digit being 0 to Z, the value can go from 0 to 36^5. (If we are comparing this with hexadecimal system, in hexadecimal each 'digit' goes from 0 to F). Now to convert this to decimal, you could try use the same method used to convert from hex or binary etc... system to the decimal system.
It will be something like d4 * (36 ^ 4) + d3 * (36 ^ 3) + d2 * (36 ^ 2) + d1 * (36 ^ 1) + d0 * (36 ^ 0)
Note: Here 36 is the total number of symbols.
d0, d1, d2, d3, d4 can range from 0 to 35 in decimal (Important: Not 0 to 36).
Also, you can extend this for any number of digits or symbols and you can implement operations like addition, subtraction etc in this system itself as well. (It will be fun to implement that. :) ) But it will be easier to convert it to decimal do the operations and convert it back though.
I have written this code to convert Decimal to binary:
string Solution::findDigitsInBinary(int A) {
if(A == 0 )
return "0" ;
else
{
string bin = "";
while(A > 0)
{
int rem = (A % 2);
bin.push_back(static_cast<char>(A % 2));
A = A/2 ;
}
reverse(bin.begin(),bin.end()) ;
return bin ;
}
}
But not getting the desired result using static_cast.
I have seen something related to this that is giving the desired result :
(char)('0'+ rem).
What's the difference between static_cast? why I am not getting the correct binary output?
With:
(char) '0' + rem;
The important difference is not the cast, but that the remainder, which always results in 0 or 1, is added to the character '0', which means that you adding a character of '0' or '1' to your string.
In your version you are adding either the integer representation of 0 or 1, but the string representations of 0 and 1 are either 48 or 49. By adding the remainder of 0 or 1 to '0' it gives a value of either 48 (character 0) or 49 (character 1).
If you do the same thing in your code it will also work.
string findDigitsInBinary(int A) {
if (A == 0)
return "0";
else
{
string bin = "";
while (A > 0)
{
int rem = (A % 2);
bin.push_back(static_cast<char>(A % 2 + '0')); // Remainder + '0'
A = A / 2;
}
reverse(bin.begin(), bin.end());
return bin;
}
Basically you should be adding characters to the string, and not numbers. So you shouldn't be adding 0 and 1 to the string, you should be adding the numbers 48 (character 0) and 49 (character 1).
This chart might illustrate better. See how the character value/digit '0' is 48 in decimal? Let's just say you wanted to add the digit 4 to the string, then because decimal 48 is 0, then you would actually want to add the decimal value of 52 to the string, 48 + 4. This is what the '0' + rem does. This is done automatically for you if you insert a character, that is, if you do:
mystring += 'A';
It will add an 'A' character to your string, but what it's actually doing in reality is converting that 'A' to decimal 65 and adding it to the string. What you have in your code is you're adding decimal numbers/integers 0 and 1, and these aren't characters in the Unicode/ASCII representation.
Now that you understand how characters are encoded, to cast an integer to a char does not change the decimal/integer to its character representation, but it changes the data type from int to char, a 4-byte data type (most likely) to a 1-byte data type. Your cast did the following:
After the modulo % operation you got a result of either 1 or 0 as an integer, let's just say you got a 1 remainder, it would look like this as an int:
00000000 00000000 00000000 00000001
After the cast to a char it would convert it to a one-byte data type, which would make it look like this:
00000001 // Now it's a one-byte data type
Whereas what a '1' digit looks like encoded as a string character is 49, which looks like this:
00110000
As for the difference between static_cast and c-style cast, the static_cast does compile-time checks and allows casts between certain types based on particular rules, whereas a c-style cast isn't as restrictive.
char a = 5;
int* p = static_cast<int*>(&a); // Will not compile
int* p2 = (int*)&a; // Will compile and run, but is discouraged as there are risks.
*p2 = 7; // You've written past the single byte char into 3 extra bytes, which is an access violation, or undefined behaviour.
So i have this hex: B0 32 B6 B4 37
I know this hex is obfuscated with some key/algorithm.
I also know this hex is equal to: 61 64 6d 69 6e (admin)
How can i calculate the XOR key for this?
If you write out the binary representation, you can see the pattern:
encoded decoded
10110000 -> 01100001
00110010 -> 01100100
Notice that the bit patterns have the same number of bits before and after. To decode, you just bitwise rotate one bit left. So the value shifts left one place and the most significant bit wraps around to the least significant place. To encode, just do the opposite.
int value, encoded_value;
encoded_value = 0xB0;
value = ((encoded_value << 1) | (encoded_value >> 7)) & 255;
// value will be 0x61;
encoded_value = ((value >> 1) | (value << 7)) & 255;
I'm tryin to get the serial number of an Acer monitor looking into the windows registry.
I'm parsing the registry with this code in Python 3:
import winreg
from winreg import HKEY_LOCAL_MACHINE
subKey = "SYSTEM\CurrentControlSet\Enum\DISPLAY"
k = winreg.OpenKey(HKEY_LOCAL_MACHINE, subKey)
with winreg.OpenKey(HKEY_LOCAL_MACHINE, subKey) as k:
""""
Open the key 'HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY'
to get the info of all connected monitors
"""
i = 0
while True:
try:
with winreg.OpenKey(k, winreg.EnumKey(k, i)) as sk:
j = 0
while True:
try:
with winreg.OpenKey(sk, winreg.EnumKey(sk, j)) as ssk:
l = 0
while True:
try:
if (winreg.EnumKey(ssk, l) == "Control"):
try:
with winreg.OpenKey(ssk, "Device Parameters") as sssk:
strEDID = str(winreg.EnumValue(sssk, 0)[1])
try:
modelo = strEDID[strEDID.index("\\x00\\x00\\x00\\xfc") + len("\\x00\\x00\\x00\\xfc\\x00"):].split("\\")[0]
serie = strEDID[strEDID.index("\\x00\\x00\\x00\\xff") + len("\\x00\\x00\\x00\\xff\\x00"):].split("\\")[0]
except:
modelo = "Not Found"
serie = "Not Found"
print ("Modelo:", modelo)
print ("Serie:", serie, "\n")
fo = open("salTest.txt", "a")
fo.write(modelo + "\n")
fo.write(serie + "\n\n")
fo.close()
except OSError:
print ("Error")
break
else:
l += 1
except OSError:
break
j += 1
except OSError:
break
i += 1
except OSError:
break
As result i get an output in the cmd window like this:
Modelo: AL1716
Serie: L4802017396L
The problem is that the "Serie" isn't the real serial number (an Acer monitor serial number have 22 characters and looks like "ETL480201781700F4B396L")
There is a way to build the real serial number with the "Serie" and the SNID that identify the monitor too.
Here is an example of two Acer monitors:
S/N ORIGINAL: ETL48020178170 (0F4B)396L | # ETL480201781700F4B396L
------------------------------------------------------------------------------------
SNID: 8170 (0F4B)=03915 | 39 # 81700391539
S/N FROM SCRIPT: L4802017 396L | # L4802017396L
S/N ORIGINAL: ETL48020178170 (2C98)396L | # ETL480201781702C98396L
------------------------------------------------------------------------------------
SNID: 8170 (2C98)=11416 | 39 # 81701141639
S/N FROM SCRIPT: L4802017 396L | # L4802017396L
Anyone know how to get this info?
Thanks!
Acer provides the serial number after the 000000ff00 flag but the middle part of the serial number is hidden earlier in the EDID string.
So for example our EDID string looks like this:
00ffffffffffff0004723a03c4fe603324170103682f1e78ca9265a655559f280d5054bfef80714f8140818081c0810095000101010126399030621a274068b03600da281100001c000000fd00374c1e5011000a202020202020000000fc0042323236574c0a202020202020000000ff004c58565341303031383531300a007b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The serial number we want is this:
LXVSA0013360FEC48510
The first 8 characters of the serial number LXVSA001 is encoded as a hex string immediately after the '000000ff00' flag.
The last 4 characters of the serial number 8510 is encoded as a hex string after this first 8 characters.
000000ff00 4c|58|56|53|41|30|30|31|38|35|31|30|0a| <-- EDID (hex)
L X V S A O 0 1 8 5 1 0 (linefeed) <-- ascii
(^^^^ first part ^^^^^^)(last part)
Now the tricky middle part 3360fec4 is encoded as 4 strings earlier in the EDID.
33 is at position 30
60 is at position 28
fe is at position 26
c4 is at position 24
00ffffffffffff0004723a03
position 24 -> c4
position 26 -> fe
position 28 -> 60
position 30 -> 33
24170103682f1e78ca9265a655559f etc
When I say 'position' I mean take the EDID string as an array and index from 0.
They are hard to find because they are in reverse order.
In your example, the missing parts of your serial number 81700F4B should be located as 4 separate 2 character strings at locations 30, 28, 26, and 24 of your idid string. I can't test that because I don't have the full IDID.
How should I do base 10 to base 16 integer conversion in Squirrel? In Javascript I can do this with: parseInt("ff", 16).
I'm trying to do a HEX color code to RGB calculator for an Electric Imp. #ffaaccwould be split into 3 parts (ff, aa and cc). I would then calculate these to base 10 integers and achieve RGB(255, 170, 204). These numbers I will then use to control an RGB led with PWM.
Try String tointeger() function.
local s = "ff";
print (s.tointeger(16));
If you want convert conversely, try format() function.
local i = 255;
print (format("%x", i));
Here's one approach using array.find (and format for reversal):
local lookup = ['0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f']
local hex = "7f"
local dec = lookup.find(hex[0]) * 0x10 + lookup.find(hex[1])
server.log(format("%s -> %d -> %02x", hex, dec, dec))