I have been trying to find a way to remap my keyboard and send 5-digit hex unicode chars, the method described here: ahk Send only supports 4-digit hex codes {U+nnnn}, I know that in the past, autohotkey didnt support unicode natively so it was needed some functions in order to do that, maybe thats the solution for me.
Example:
#If GetKeyState("CapsLock","T")
+u::Send {U+1D4B0}
The results from that is 풰 instead of 𝒰, and the code for 풰 is {U+D4B0}, meaning AHK is reading only the last 4 digits. How can I fix it even if I need to make new functions to achieve that?
Thanks
-Mark
Unicode values larger than 0xFFFF must be encoded as two surrogate pairs:
+u:: SendInput ,{U+D835}{U+DCB0}
Here is the algorithm to convert a Unicode code point that is in range 0x10000 to 0x10FFFF, to surrogate pairs, paraphrased from wikipedia:
First subtract 0x10000 from the code point, to get a number in range 0xFFFFF.
Then right shift the number by 10 bits and add 0xD800 to it to get the high surrogate.
Take the lowest ten bits of the number and add 0xDC00 to it to get the low surrogate
I took 2501's solution from above and turned it into working a working ahk script. I have been searching on and off for this solution for months!
+u:: FUNC_SEND_UNICODE( 0x1D4B0 )
FUNC_SEND_UNICODE(func_args)
{
/*
;commented out proof of concept code:
str := ""
u1 := Chr( 0xD835 )
u2 := Chr( 0xDCB0 )
str := u1 . u2
bk := clipboard
clipboard := str
send, ^v
sleep,200
clipboard := bk
*/
;bk == "clipboard [B]ac[K]up
bk := clipboard
;chunk of data that needs to be cut into
;two chunks.
ful := func_args + 0
/* commented out testing code, using original
sample input of stack overflow post
;msgbox,ful:[%ful%]
;for testing. Expecting input to be equal to
;the value in the post.
if(ful != 0x1D4B0 ){
msgbox,"[WHATTHEHECK]"
}
*/
;Subtract 0x10000 from ful, gets number
;in range(rng) 0x0 to 0xFFFFFF inclusive
rng := ful - 0x10000
if(rng > 0xFFFFF)
{
msgBox,[Step1 Resulted In Out Of Range]
}
;Do shifting and masking, then check to make
;sure the value is in the expected range:
big_shif := (rng >> 10)
lit_mask := (rng & 0x3FF)
if(big_shif > 0x3FF)
{
msgBox,[MATH_ERROR:big_shif]
}
if(lit_mask > 0x3FF)
{
msgBox,[MATH_ERROR:lit_mask]
}
big := big_shif + 0xD800
lit := lit_mask + 0xDC00
if(big < 0xD800 || big >= 0xDBFF){
msgBox, [HIGH_SURROGATE_OUT_OF_BOUNDS]
}
if(lit < 0xDC00 || lit >= 0xDFFF){
msgBox, [LOW_SURROGATE_OUT_OF_BOUNDS]
}
;Convert code points to actual characters:
u1 := Chr( big )
u2 := Chr( lit )
;concatentate those two characters to
;create our final surrogate output:
str := u1 . u2
;set it equal to clipboard, and send
;the clipboard. This is a hack.
;send,%str% works fine in google chrome,
;but fails in notepad++ for some reason.
;tried appending EOF, STX, ETX control codes
;along with output, but to no effect.
clipboard := str
send, ^v
;Must sleep before restoring clipboard,
;otherwise, the clipboard will get
;overwritten before ctrl+v has the chance
;to output the character. You'll end up just
;pasting whatever was originally on the
;clipboard.
sleep,200
clipboard := bk
return
}
An implementation of 2501's answer.
Listens for ;u
Followed by an ending character (e.g. ; or Return)
Waits for a sequence of keypresses like 1D4B0
Input ends when terminated by a ;
Sends the desired Unicode character directly if below 65536, calculates surrogate pairs otherwise
Inserts the desired Unicode character
:?:`;u::
Input, keys, , `;
if (StrLen(keys) < 5)
Send {U+%keys%}
else {
keys := "0x" + keys
num := keys - 0x10000
w1 := Format("{:x}", (num >> 10) + 0xD800)
w2 := Format("{:x}", (num & 1023) + 0xDC00)
Send {U+%w1%}{U+%w2%}
}
return
For reference:
Hotstrings
Input
StrLen
Casting string to numeric
Format
Bitwise operations
Send
From somewhere on the internet: sendunicodechar(0x1D4B0)
SendUnicodeChar(charCode)
{
VarSetCapacity(ki, 28 * 2, 0)
EncodeInteger(&ki + 0, 1)
EncodeInteger(&ki + 6, charCode)
EncodeInteger(&ki + 8, 4)
EncodeInteger(&ki +28, 1)
EncodeInteger(&ki +34, charCode)
EncodeInteger(&ki +36, 4|2)
DllCall("SendInput", "UInt", 2, "UInt", &ki, "Int", 28)
}
EncodeInteger(ref, val)
{
DllCall("ntdll\RtlFillMemoryUlong", "Uint", ref, "Uint", 4, "Uint", val)
}
edit. probably got downvoted bc of missing source. I simply dont know where I got it from anymore. But when I used it a few years ago, it worked flawlessly.
Related
This question already has answers here:
How do I get the bits from a byte in golang?
(5 answers)
Closed 8 months ago.
I have a hash:
b := hash.Sum(nil)
I am really just interested in the first bit of that byte. Is it 0 or 1?
So far I have this:
s := strconv.FormatInt(int64(b[0]),2)
if s[0] == '0' {
// it's 0
} else {
// it's 1
}
But I am sure there is a much more elegant (and more performant?) way to do this.
you could just check the result of bitwise and operator
if b[0] & 0x1 == 0x1 {
// it's 1
} else {
// it's 0
}
The Sum() function of a hash returns a slice of bytes. You want to isolate the first bit of the first byte, correct?
It's a simple matter of bit-twiddling. 2 or 3 machine instructions at most.
Depending on what you mean by "first bit",
This gives you the high-order/most significant/leftmost bit of a byte:
func HighOrderBit(b byte) byte {
return (b >> 7) & 0x01
}
And this gives you the low-order/least significant/rightmost bit of a byte:
func LowOrderBit(b byte) byte {
return (b >> 0) & 0x01
}
Note that the above works for any integer type: the only difference being the size of the right shift in highOrderBit(). The number of bits to shift right is the size of the integer type in bits minus one (e.g., for a 64-bit integer, the shift value is 64-1, or 63).
When doing bitwise not, get a lot ffffffff. How to do correctly?
space := " "
str := "12345678999298765432179.170.184.81"
sp := len(str) % 4
if sp > 0 {
str = str + space[0:4-sp]
}
fmt.Println(str, len(str))
hx := hex.EncodeToString([]byte(str))
ln := len(hx)
a, _ := strconv.ParseUint(hx[0:8], 16, 0)
for i := 8; i < ln; i += 8 {
b, _ := strconv.ParseUint(hx[i:i+8], 16, 0)
a = a ^ b
}
xh := strconv.FormatUint(^a, 16)
fmt.Println(xh)
output
ffffffffc7c7dbcb
I need only
c7c7dbcb
You get a lot of leading ff because your a number in fact is only 32-bit "large" but is used "within" a 64-bit uint64 value. (You're processing numbers with 8 hex digits = 4 bytes data = 32 bit.) It has 4 leading 0 bytes, which when negated will turn into ff. You can verify this with:
fmt.Printf("a %#x\n",a)
Outputs:
a 0x38382434
To get rid of those leading ff, convert the result to uint32:
xh := strconv.FormatUint(uint64(uint32(^a)), 16)
fmt.Println(xh)
(Converting back to uint64 is because strconv.FormatUint() expects / requires uint64.)
This outputs:
c7c7dbcb
Another option is to apply a 0xffffffff bitmask:
xh = strconv.FormatUint(^a&0xffffffff, 16)
fmt.Println(xh)
Also note that you could print it using fmt.Printf() (or fmt.Sprintf() if you need it as a string) where you specify %08x verb which also adds leading zeros should the input has more than 3 leading 0 bits (and thus strconv.FormatUint() would not add leading hex zeros):
fmt.Printf("%08x", uint32(^a))
This outputs the same. Try the examples on the Go Playground.
type IntSet struct {
words []uint64
}
func (s *IntSet) Has(x int) bool {
word, bit := x/64, uint(x%64)
return word < len(s.words) && s.words[word]&(1<<bit) != 0
}
Lets go through what I think is going on:
A new type is declared called IntSet. Underneath its new type declaration it is unint64 slice.
A method is created called Has(). It can only receive IntSet types, after playing around with ints she returns a bool
Before she can play she needs two ints. She stores these babies on the stack.
Lost for words
This methods purpose is to report whether the set contains the non-negative value x. Here is a the go test:
func TestExample1(t *testing.T) {
//!+main
var x, y IntSet
fmt.Println(x.Has(9), x.Has(123)) // "true false"
//!-main
// Output:
// true false
}
Looking for some guidance understanding what this method is doing inside. And why the programmer did it in such complicated means (I feel like I am missing something).
The return statement:
return word < len(s.words) && s.words[word]&(1<<bit) != 0
Are the order of operations this?
return ( word < len(s.words) && ( s.words[word]&(1<<bit)!= 0 )
And what is the [words] and & doing within:
s.words[word]&(1<<bit)!= 0
edit: Am beginning to see slightly seeing that:
s.words[word]&(1<<bit)!= 0
Is just a slice but don't understand the &
As I read the code, I scribbled some notes:
package main
import "fmt"
// A set of bits
type IntSet struct {
// bits are grouped into 64 bit words
words []uint64
}
// x is the index for a bit
func (s *IntSet) Has(x int) bool {
// The word index for the bit
word := x / 64
// The bit index within a word for the bit
bit := uint(x % 64)
if word < 0 || word >= len(s.words) {
// error: word index out of range
return false
}
// the bit set within the word
mask := uint64(1 << bit)
// true if the bit in the word set
return s.words[word]&mask != 0
}
func main() {
nBits := 2*64 + 42
// round up to whole word
nWords := (nBits + (64 - 1)) / 64
bits := IntSet{words: make([]uint64, nWords)}
// bit 127 = 1 * 64 + 63
bits.words[1] = 1 << 63
fmt.Printf("%b\n", bits.words)
for i := 0; i < nWords*64; i++ {
has := bits.Has(i)
if has {
fmt.Println(i, has)
}
}
has := bits.Has(127)
fmt.Println(has)
}
Playground: https://play.golang.org/p/rxquNZ_23w1
Output:
[0 1000000000000000000000000000000000000000000000000000000000000000 0]
127 true
true
The Go Programming Language Specification
Arithmetic operators
& bitwise AND integers
peterSO's answer is spot on - read it. But I figured this might also help you understand.
Imagine I want to store some random numbers in the range 1 - 8. After I store these numbers I will be asked if the number n (also in the range of 1 - 8) appears in the numbers I recorded earlier. How would we store the numbers?
One, probably obvious, way would be to store them in a slice or maybe a map. Maybe we would choose a map since lookups will be constant time. So we create our map
seen := map[uint8]struct{}{}
Our code might look something like this
type IntSet struct {
seen: map[uint8]struct{}
}
func (i *IntSet) AddValue(v uint8) {
i.seen[v] = struct{}{}
}
func (i *IntSet) Has(v uint8) bool {
_, ok := i.seen[v]
return ok
}
For each number we store we take up (at least) 1 byte (8 bits) of memory. If we were to store all 8 numbers we would be using 64 bits / 8 bytes.
However, as the name implies, this is an int Set. We don't care about duplicates, we only care about membership (which Has provides for us).
But there is another way we could store these numbers, and we could do it all within a single byte. Since a byte provides 8 bits, we can use these 8 bits as markers for values we have seen. The initial value (in binary notation) would be
00000000 == uint8(0)
If we did an AddValue(3) we could change the 3rd bit and end up with
00000100 == uint8(3)
^
|______ 3rd bit
If we then called AddValue(8) we would have
10000100 == uint8(132)
^ ^
| |______ 3rd bit
|___________ 8th bit
So after adding 3 and 8 to our IntSet we have the internally stored integer value of 132. But how do we take 132 and figure out whether a particular bit is set? Easy, we use bitwise operators.
The & operator is a logical AND. It will return the value of the bits common between the numbers on each side of the operator. For example
10001100 01110111 11111111
& 01110100 & 01110000 & 00000001
-------- -------- --------
00000100 01110000 00000001
So to find out if n is in our set we simply do
our_set_value & (1 << (value_we_are_looking_for - 1))
which if we were searching for 4 would yield
10000100
& 00001000
----------
0 <-- so 4 is not present
or if we were searching for 8
10000100
& 10000000
----------
10000000 <-- so 8 is present
You may have noticed I subtracted 1 from our value_we_are_looking for. This is because I am fitting 1-8 into our 8bit number. If we only wanted to store seven numbers then we could just skip using the very first bit and assume our counting starts at bit #2 then we wouldn't have to subtract 1, like the code you posted does.
Assuming you understand all of that, here's where things get interesting. So far we have been storing our values in a uint8 (so we could only have 8 values, or 7 if you omit the first bit). But there are larger numbers that have more bits, like uint64. Instead of 8 values, we can store 64 values! But what happens if the range of values we want to track exceed 1-64? What if we want to store 65? This is where the slice of words comes from in the original code.
Since the code posted skips the first bit, from now on I will do so as well.
We can use the first uint64 to store the numbers 1 - 63. When we want to store the numbers 64-127 we need a new uint64. So our slice would be something like
[ uint64_of_1-63, uint64_of_64-127, uint64_of_128-192, etc]
Now, to answer the question about whether a number is in our set we need to first find the uint64 whose range would contain our number. If we were searching for 110 we would want to use the uint64 located at index 1 (uint64_of_64-128) because 110 would fall in that range.
To find the index of the word we need to look at, we take the whole number value of n / 64. In the case of 110 we would get 1, which is exactly what we want.
Now we need to examine the specific bit of that number. The bit that needs to be checked would be the remainder when dividing 110 by 64, or 46. So if the 46th bit of the word at index 1 is set, then we have seen 110 before.
This is how it might look in code
type IntSet struct {
words []uint64
}
func (s *IntSet) Has(x int) bool {
word, bit := x/64, uint(x%64)
return word < len(s.words) && s.words[word]&(1<<bit) != 0
}
func (s *IntSet) AddValue(x int) {
word := x / 64
bit := x % 64
if word < len(s.words) {
s.words[word] |= (1 << uint64(bit))
}
}
And here is some code to test it
func main() {
rangeUpper := 1000
bits := IntSet{words: make([]uint64, (rangeUpper/64)+1)}
bits.AddValue(127)
bits.AddValue(8)
bits.AddValue(63)
bits.AddValue(64)
bits.AddValue(998)
fmt.Printf("%b\n", bits.words)
for i := 0; i < rangeUpper; i++ {
if ok := bits.Has(i); ok {
fmt.Printf("Found %d\n", i)
}
}
}
OUTPUT
Found 8
Found 63
Found 64
Found 127
Found 998
Playground of above
Note
The |= is another bitwise operator OR. It means combine the two values keeping anywhere there is a 1 in either value
10000000 00000001 00000001
& 01000000 & 10000000 & 00000001
-------- -------- --------
11000000 10000001 00000001 <-- important that we
can set the value
multiple times
Using this method we can reduce the cost of storage for 65535 numbers from 131KB to just 1KB. This type of bit manipulation for set membership is very common in implementations of Bloom Filters
An IntSet represents a Set of integers. The presence in the set of any of a contiguous range of integers can be established by writing a single bit in the IntSet. Likewise, checking whether a specific integer is in the IntSet can be done by checking whether the particular integer corresponding to that bit is set.
So the code is finding the specific uint64 in the Intset corresponding to the integer:
word := x/64
and then the specific bit in that uint64:
bit := uint(x%64)
and then checking first that the integer being tested is in the range supported by the IntSet:
word < len(s.words)
and then whether the specific bit corresponding to the specific integer is set:
&& s.words[word]&(1<<bit) != 0
This part:
s.words[word]
pulls out the specific uint64 of the IntSet that tracks whether the integer in question is in the set.
&
is a bitwise AND.
(1<<bit)
means take a 1, shift it to the bit position representing the specific integer being tested.
Performing the bitwise AND between the integer in question, and the bit-shifted 1 will return a 0 if the bit corresponding to the integer is not set, and a 1 if the bit is set (meaning, the integer in question is a member of the IntSet).
I am trying to implement socks5 proxy server.
Most things are clear according to the rfc but I'm stuck interpreting client port and writing my port number in bytes.
I made a function that tkes an int and returns 2 bytes. This function first converts number into binary then literally splits the bits as string then converts them back to byte.However this seems wrong because if the right most bits are 0 they are lost.
Here is the function
func getBytesOfInt(i int) []byte {
binary := fmt.Sprintf("%b", i)
if i < 255 {
return []byte{byte(i)}
}
first := binary[:8]
last := binary[9:]
fmt.Println(binary, first, last)
i1, _ := strconv.ParseInt(first, 2, 64)
i2, _ := strconv.ParseInt(last, 2, 64)
return []byte{byte(i1), byte(i2)}
}
Can you please explain me how am i supposed to parse the number and get 2 bytes and most importantly how am i going to cast it back to an integer.
Currently if you give 1024 to this function it will return []byte{0x80, 0x0} which is 128 in decimals but as you see the right bits are lost theres only one 0 which is useless.
Your code has multiple problem. First :8 and 9: miss an element ([8]), see: https://play.golang.org/p/yuhh4ZeJFNL
And also, you should interept the second byte as lowbyte of the int and the first as highbyte, not literally cut the binary string. for example 4 should be interept as [0x0,0x4] instead of [0x4,0x0] which shoulld be 1024.
If you want to keep using strconv you should use:
n := len(binary)
first := binary[:n-8]
last := binary[n-8:]
However it is very unefficient.
I would suggest b[0],b[1] = i >> 8, i & 255, and i = b[0]<<8 + b[1] .
I have this function:
Private Sub CheckParams(Values)
Dim Str, Ch
If IsArray(Values) then
Str = Join(Values, "")
Else
Str = Values
End If
For I = 1 To Len(Str)
Ch = Asc(Mid(Str, I, 1))
If Not ((Ch = 9) Or (Ch = 10) Or (Ch = 13) Or ((Ch > 31) And (Ch < 128))) Then
SetError("script result contains illegal characters.")
End If
Next
End Sub
This function throws error if the input value has characters that not on the list according to the If statement in the For loop. The problem is when my input value has Japanese characters, the validation is passed without error. I think the Asc() function, that use to return the ANSI code of the character, don't know how to handle the Japanese characters. What seems to be the problem here? Is the Asc() function returns negative numbers?
Kanji and Kana are most likely represented as 2-Byte Unicode characters, so you could try something like this:
ch = AscW(MidB(str, i, 2))
I found the solution. It is very similar to Ansgar's solution but instead MidB I used Mid with 1:
ch = AscW(Mid(str, i, 1))