; 字符串转16进制码
MsgBox, % Chr(116) ; 显示 "t"【自带函数方法】

Str := "ABCD"
VarSetCapacity(hex, StrLen(Str) * 6)
DllCall("crypt32\CryptBinaryToString", "astr", str, "uint", StrLen(str), "uint", 0x40000004, "str", hex, "uint*", StrLen(Str) * 3)
MsgBox % hex

; WinAPI来16进制转成ascii码
Str := "48656c6c6f"
DllCall("crypt32\CryptStringToBinaryW", "ptr", &str, "uint", StrLen(str), "uint", 0x4, "ptr", 0, "uint*", bytes, "ptr", 0, "ptr", 0)
VarSetCapacity(buf, bytes)
DllCall("crypt32\CryptStringToBinaryW", "ptr", &str, "uint", StrLen(str), "uint", 0x4, "ptr", &buf, "uint*", bytes, "ptr", 0, "ptr", 0)
MsgBox % StrGet(&buf, "utf-8")

/*      封装成函数
Str := "01 05 00 00 FF 00 8C ЗA"
bytes := CryptStringToBinary(Str , buf)
myTcp.Send(&buf ,bytes)

CryptStringToBinary(Str, ByRef buf, flag:=0x4) {
    DllCall("crypt32\CryptStringToBinaryW", "ptr", &Str, "uint", StrLen(Str), "uint", flag, "ptr", 0, "uint*", bytes, "ptr", 0, "ptr", 0)
    VarSetCapacity(buf, bytes)
    DllCall("crypt32\CryptStringToBinaryW", "ptr", &Str, "uint", StrLen(Str), "uint", flag, "ptr", &buf, "uint*", bytes, "ptr", 0, "ptr", 0)
    Return bytes
}
*/

; 两个字母分割再枚举
字符 :="48656c6c6f"
for k, v in StrSplit(RTrim(RegExReplace(字符, "(\w\w)", "$1 ")), A_Space)
  a .=Chr("0x" v)
MsgBox % a


; 以空格为分割16进制转成ascii码
字符 := "48 65 6c 6c 6f"
;MSgBox, % Chr(0x74) ;显示"t"
for index, v in StrSplit(字符, A_Space)
  a .=Chr("0x" v)
MsgBox % a

VarSetCapacity(binary, 8, 0)
NumPut(0x01, binary, 0, "UChar")
NumPut(0x05, binary, 1, "UChar")
NumPut(0x00, binary, 2, "UChar")
NumPut(0x00, binary, 3, "UChar")
NumPut(0xFF, binary, 4, "UChar")
NumPut(0x00, binary, 5, "UChar")
NumPut(0x8c, binary, 6, "UChar")
NumPut(0x3a, binary, 7, "UChar")
; myTcp.Send(&binary, 8)  ; Tcp发送

; bytes := newTcp.recv(buf)  ; Tcp接收
MsgBox % CryptBinaryToString(&binary, 8)  ; 接收发送端的16进制信息



; 转换并返回收到的16进制信息 By Tebayaki
CryptBinaryToString(addr, bytes, flag := 0x40000004) {
    DllCall("crypt32\CryptBinaryToStringW", "ptr", addr, "uint", bytes, "uint", flag, "ptr", 0, "uint*", chars)
    VarSetCapacity(hex, chars * 2)
  DllCall("crypt32\CryptBinaryToStringW", "ptr", addr, "uint", bytes, "uint", flag, "str", hex, "uint*", chars)
  Return hex
}


; V2代码
; str := buffer(2)
; NumPut("char", 0x74, "char", 0x75, str, 0)
; MsgBox StrGet(str, 2, "utf-8")


/*******************************V2代码 聊天记录*************************************
buffer是啥意思?

缓存对象,在v1里用varsetcapacity

@Tebayaki+AHK2 大佬遇到问题了,v2buffer指定位置分段写入怎么搞      字符串

取址然后加偏移量

没法写地址

struct_item.ptr
*/

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。