FormatBytes()
Re: FormatBytes()
Windows 10.0.14388
2400016 Bytes => 2.28 MB (so still 1024)
M$ should move his WinAPI docs to GitHub...
2400016 Bytes => 2.28 MB (so still 1024)
M$ should move his WinAPI docs to GitHub...
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: FormatBytes()
Code: Select all
FormatBytes(bytes){
Loop,Parse,bytes|KB|MB|GB|TB|PB|EB|ZB|YB,|
If bytes>999,bytes/=1024
else return RTrim(SubStr(bytes,1,4),".") " " A_LoopField
}
Code: Select all
FormatBytes(bytes){
Loop
If bytes>999,bytes/=1024
else return RTrim(SubStr(bytes,1,4),".") " " SubStr("BYKBMBGBTBPBEBZBYB",A_Index*2-1,2)
}
Last edited by arcticir on 14 Jul 2016, 09:51, edited 1 time in total.
Re: FormatBytes()
Whewwww!
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: FormatBytes()
arcticir's code does not work on AutoHotkey v1, only v2.
Here's another version.
Here's another version.
Code: Select all
FormatBytes(bytes){
Loop
If bytes > 999
bytes /= 1024.0
else return RTrim(SubStr(bytes,1,4),".") " " (A_Index=1 ? "bytes" : SubStr(" KMGTPEZY",A_Index,1) "B")
}
Re: FormatBytes()
Nice
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
FormatBytes() - Updated
Previous code:
is now simpliified to a one liner:
Code: Select all
FormatBytes( Integer, Suffix := "" ) { ; SKAN, http://ahkscript.org/boards/viewtopic.php?p=18328#p18328
Static Var := "0123456789ABCDEF"
Return DllCall( "Shlwapi\StrFormatByteSize64A", "Int64",Integer, "Str",Var, "UInt",16, "AStr" ) . Suffix
}
is now simpliified to a one liner:
Code: Select all
FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
}
Re: FormatBytes()
Neat work as always!
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: FormatBytes()
I made a Function to reverse this operation but it doesn't work on small numbers. If you have a better one, it would be great if you could share it.
Code: Select all
ToByte(SizeAndSpaceAndUnit){ ;by hasantr
static Units := {KB:1,MB:2,GB:3,TB:4}
SplitN := StrSplit(SizeAndSpaceAndUnit,A_Space,"`n")
Loop % Units[SplitN.2]
SplitN.1 := SplitN.1 * 1024
Return SplitN.1
}
Re: FormatBytes()
Does this help?
Code: Select all
n:=9223372036854775807 ; B = 7,99 EB
;n:=200
x:= FormatBytes(n)
m:= FormatStringToByte(x)
tooltip, % n " B = " x " = " m " B"
sleep,4000
return
FormatStringToByte(unitstrg){
Units := {B:0, Bytes:0, KB:10, MB:20, GB:30,TB:40, PB:50, EB:60}
RegExMatch(unitstrg, "im)(\d+,?\d{0,})\s{0,}(Bytes|KB|MB|GB|TB|PB|EB)",SubPat)
SubPat1:=StrReplace(SubPat1,",",".")
Return round(subPat1 * 2**Units[subPat2])
}
FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes
Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr")
}
Re: FormatBytes()
Thanks. This is better.haichen wrote: ↑20 Mar 2022, 09:07Does this help?Code: Select all
n:=9223372036854775807 ; B = 7,99 EB ;n:=200 x:= FormatBytes(n) m:= FormatStringToByte(x) tooltip, % n " B = " x " = " m " B" sleep,4000 return FormatStringToByte(unitstrg){ Units := {B:0, Bytes:0, KB:10, MB:20, GB:30,TB:40, PB:50, EB:60} RegExMatch(unitstrg, "im)(\d+,?\d{0,})\s{0,}(Bytes|KB|MB|GB|TB|PB|EB)",SubPat) SubPat1:=StrReplace(SubPat1,",",".") Return round(subPat1 * 2**Units[subPat2]) } FormatBytes(N) { ; By SKAN on CT5H/D351 @ tiny.cc/formatbytes Return DllCall("Shlwapi\StrFormatByteSize64A", "Int64",N, "Str",Format("{:16}",N), "Int",16, "AStr") }