惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
The Cloudflare Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
I
InfoQ
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
H
Help Net Security
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

6Jyc5p+a

【笔记】HermesAgent学习笔记 【笔记】DeepSeekHarness学习笔记 【笔记】Rollup的PluginBabel插件 【笔记】Rollup学习笔记 【笔记】Gulp的BrowserSync插件 【笔记】Node-SSH学习笔记 【笔记】Tapable学习笔记 【笔记】Highlight.js学习笔记 【笔记】Marked学习笔记 【笔记】Webpack通过schema-utils实现Loader参数校验 【笔记】Webpack实现TreeShaking 【笔记】Webpack的WebpackBundleAnalyzer插件 【代码】校验信用卡号码 【笔记】Webpack的SpeedMesureWebpackPlugin插件 【笔记】Codex配置Brave浏览器的MCP 【笔记】Webpack的CSSMinimizerWebpackPlugin插件 【笔记】FastNodeManager学习笔记 【笔记】Terser学习笔记 【笔记】Codex配置Wireshark的MCP 【笔记】Codex配置Unity的MCP 【笔记】Codex配置Blender的MCP 【笔记】Codex配置IDEA的MCP 【笔记】Browserslist学习笔记 【笔记】Codex学习笔记 【笔记】MicrosoftStore微软商店学习笔记 【笔记】通过NapCat和AstrBot实现QQ机器人 【笔记】部署NapCat 【笔记】部署AstrBot 【笔记】Poetry学习笔记 【笔记】uv学习笔记
【代码】JS将目录编号转换为十六进制
6Jyc5p+a · 2026-05-19 · via 6Jyc5p+a

前言

JS将目录编号转换为十六进制

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class HexToc {

constructor(bitValueArrayLength, singleBitLength, config) {

const defaultConfig = {





reverse: false,




noPrefix: false,




separator: ""
}





this.bitValueArray = [0];





this.singleBitLength = 2;




this.config = {
...defaultConfig,
...config
};

if (typeof bitValueArrayLength === "number" && bitValueArrayLength > 0) {
this.#resizeBitValueArray(bitValueArrayLength);
}

if (typeof singleBitLength === "number" && singleBitLength > 0) {
this.singleBitLength = singleBitLength;
}
}







#resizeBitValueArray(length) {
const increment = length - this.bitValueArray.length;
if (increment < 0) {
console.error("不能缩减 bitValueArray 长度");
return;
}
for (let i = 0; i < increment; i++) {
this.bitValueArray.push(0);
}
}







#convertDecToHex(dec) {
if (typeof dec !== "number") {
console.error("参数 dec 必须为数值类型");
return;
}
return dec.toString(16).padStart(this.singleBitLength, "0").slice(-this.singleBitLength).toUpperCase();
}




toString() {
let res = "";
for (const dec of this.bitValueArray) {
res = this.config.reverse ? `${res}${this.config.separator}${this.#convertDecToHex(dec)}` : `${this.#convertDecToHex(dec)}${this.config.separator}${res}`;
}
return this.config.noPrefix ? res : `0x${res}`;
}




getRawData() {
return this.bitValueArray;
}







setBitValueByArrayIndex(arrayIndex, dec) {
if (arrayIndex < 0) {
console.error("arrayIndex 不能小于 0");
return;
}
if (arrayIndex > this.bitValueArray.length) {
console.error("arrayIndex 不能大于 bitValueArray 长度");
return;
}
this.bitValueArray[arrayIndex] = dec;
return this;
}







setBitValueByTocDepth(tocDepth, dec) {
if (tocDepth - 1 < 0) {
console.error("tocDepth - 1 不能小于 0\n或许你应该使用 setBitValueByArrayIndex() ?");
return;
}
if (tocDepth - 1 > this.bitValueArray.length) {
console.error("tocDepth - 1 不能大于 bitValueArray 长度");
return;
}
this.bitValueArray[tocDepth - 1] = dec;
return this;
}





setBitValueArray(decArray) {
for (let i = 0; i < decArray.length; i++) {
this.bitValueArray[i] = decArray[i];
}
return this;
}
}

完成