or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

address-types.mdformat-conversion.mdindex.mdipv4.mdipv6.mdspecial-analysis.mdsubnet-operations.md
tile.json

format-conversion.mddocs/

0

# Format Conversion

1

2

Conversion between different address representations including hexadecimal, binary, decimal, canonical, and reverse DNS formats for both IPv4 and IPv6 addresses.

3

4

## Capabilities

5

6

### IPv4 Format Conversions

7

8

Convert IPv4 addresses between various numeric and string representations.

9

10

```typescript { .api }

11

/**

12

* Convert IPv4 address to hexadecimal string with colon separators

13

* Available on Address4 instances

14

* @returns Colon-separated hex string (e.g., "c0:a8:01:01")

15

*/

16

toHex(): string;

17

18

/**

19

* Convert IPv4 address to array of integers

20

* Available on Address4 instances

21

* @returns Array of 4 integers (0-255) representing each octet

22

*/

23

toArray(): number[];

24

25

/**

26

* Convert IPv4 address to BigInt representation

27

* Available on Address4 instances

28

* @returns BigInt value of the 32-bit address

29

*/

30

bigInt(): bigint;

31

32

/**

33

* Convert IPv4 address to zero-padded binary string

34

* Available on Address4 instances

35

* @returns 32-character binary string

36

*/

37

binaryZeroPad(): string;

38

39

/**

40

* Convert IPv4 address to IPv6 group format for embedding

41

* Available on Address4 instances

42

* @returns Hex string formatted for IPv6 embedding

43

*/

44

toGroup6(): string;

45

46

/**

47

* Generate reverse DNS (in-addr.arpa) representation

48

* Available on Address4 instances

49

* @param options - Options for reverse form generation

50

* @returns Reverse DNS string

51

*/

52

reverseForm(options?: ReverseFormOptions): string;

53

```

54

55

**IPv4 Conversion Examples:**

56

57

```typescript

58

import { Address4 } from "ip-address";

59

60

const addr = new Address4("192.168.1.100");

61

62

// Hexadecimal conversion

63

console.log(addr.toHex()); // "c0:a8:01:64"

64

65

// Array conversion

66

console.log(addr.toArray()); // [192, 168, 1, 100]

67

68

// BigInt conversion

69

console.log(addr.bigInt()); // 3232235876n

70

71

// Binary conversion

72

console.log(addr.binaryZeroPad()); // "11000000101010000000000101100100"

73

74

// IPv6 group format

75

console.log(addr.toGroup6()); // "c0a8:0164"

76

77

// Reverse DNS

78

console.log(addr.reverseForm()); // "100.1.168.192.in-addr.arpa."

79

console.log(addr.reverseForm({ omitSuffix: true })); // "100.1.168.192"

80

```

81

82

### IPv6 Format Conversions

83

84

Convert IPv6 addresses between various representations including compressed, canonical, and specialized formats.

85

86

```typescript { .api }

87

/**

88

* Convert IPv6 address to correct compressed form

89

* Available on Address6 instances

90

* @returns Properly compressed IPv6 string (e.g., "2001:db8::1")

91

*/

92

correctForm(): string;

93

94

/**

95

* Convert IPv6 address to canonical (uncompressed) form

96

* Available on Address6 instances

97

* @returns Full 32-character hex with colons (e.g., "2001:0db8:0000:0000:0000:0000:0000:0001")

98

*/

99

canonicalForm(): string;

100

101

/**

102

* Convert IPv6 address to decimal representation

103

* Available on Address6 instances

104

* @returns Colon-separated decimal groups

105

*/

106

decimal(): string;

107

108

/**

109

* Convert IPv6 address to BigInt representation

110

* Available on Address6 instances

111

* @returns 128-bit BigInt value

112

*/

113

bigInt(): bigint;

114

115

/**

116

* Convert IPv6 address to zero-padded binary string

117

* Available on Address6 instances

118

* @returns 128-character binary string

119

*/

120

binaryZeroPad(): string;

121

122

/**

123

* Convert IPv6 address to byte array

124

* Available on Address6 instances

125

* @returns Array of bytes representing the address

126

*/

127

toByteArray(): number[];

128

129

/**

130

* Convert IPv6 address to unsigned byte array

131

* Available on Address6 instances

132

* @returns Array of unsigned bytes (0-255)

133

*/

134

toUnsignedByteArray(): number[];

135

136

/**

137

* Generate reverse DNS (ip6.arpa) representation

138

* Available on Address6 instances

139

* @param options - Options for reverse form generation

140

* @returns Reverse DNS string

141

*/

142

reverseForm(options?: ReverseFormOptions): string;

143

144

/**

145

* Generate Microsoft UNC literal representation

146

* Available on Address6 instances

147

* @returns Microsoft-compatible IPv6 literal format

148

*/

149

microsoftTranscription(): string;

150

```

151

152

**IPv6 Conversion Examples:**

153

154

```typescript

155

import { Address6 } from "ip-address";

156

157

const addr = new Address6("2001:0db8:0000:0000:0000:0000:0000:0001");

158

159

// Form conversions

160

console.log(addr.correctForm()); // "2001:db8::1" (compressed)

161

console.log(addr.canonicalForm()); // "2001:0db8:0000:0000:0000:0000:0000:0001" (full)

162

console.log(addr.decimal()); // "08193:03512:00000:00000:00000:00000:00000:00001"

163

164

// Numeric conversions

165

console.log(addr.bigInt()); // 42540766411282592856903984951653826561n

166

console.log(addr.binaryZeroPad()); // 128-character binary string

167

168

// Byte array conversions

169

console.log(addr.toByteArray()); // [32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

170

console.log(addr.toUnsignedByteArray()); // Same as toByteArray for IPv6

171

172

// Reverse DNS

173

console.log(addr.reverseForm());

174

// "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa."

175

176

console.log(addr.reverseForm({ omitSuffix: true }));

177

// "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2"

178

179

// Microsoft UNC format

180

console.log(addr.microsoftTranscription()); // "2001-db8-0-0-0-0-0-1.ipv6-literal.net"

181

```

182

183

### IPv4-in-IPv6 Conversions

184

185

Special conversions for IPv6 addresses containing IPv4 components.

186

187

```typescript { .api }

188

/**

189

* Extract IPv4 address from the last 32 bits

190

* Available on Address6 instances

191

* @returns Address4 instance from last 32 bits

192

*/

193

to4(): Address4;

194

195

/**

196

* Convert to IPv4-in-IPv6 representation

197

* Available on Address6 instances

198

* @returns String with IPv4 notation in the last group

199

*/

200

to4in6(): string;

201

202

/**

203

* Convert to 6to4 address format if applicable

204

* Available on Address6 instances

205

* @returns Address6 instance in 6to4 format, or null if not v4-in-v6

206

*/

207

to6to4(): Address6 | null;

208

```

209

210

**IPv4-in-IPv6 Examples:**

211

212

```typescript

213

// IPv4-mapped IPv6 address

214

const mapped = new Address6("::ffff:192.168.1.100");

215

216

console.log(mapped.to4().correctForm()); // "192.168.1.100"

217

console.log(mapped.to4in6()); // "::ffff:192.168.1.100"

218

219

// Any IPv6 can extract last 32 bits as IPv4

220

const regular = new Address6("2001:db8:1234:5678::abcd");

221

console.log(regular.to4().correctForm()); // "0.0.171.205" (from ::abcd)

222

223

// 6to4 conversion (only works for v4-in-v6 addresses)

224

const v4inv6 = new Address6("::ffff:203.0.113.1");

225

const sixToFour = v4inv6.to6to4();

226

if (sixToFour) {

227

console.log(sixToFour.correctForm()); // "2002:cb00:7101::/16"

228

}

229

230

// Regular addresses return null for 6to4

231

const regularAddr = new Address6("2001:db8::1");

232

console.log(regularAddr.to6to4()); // null

233

```

234

235

### Cross-Format Creation

236

237

Create addresses from various input formats using static methods.

238

239

```typescript { .api }

240

// IPv4 static creation methods

241

static fromHex(hex: string): Address4;

242

static fromInteger(integer: number): Address4;

243

static fromBigInt(bigInt: bigint): Address4;

244

static fromArpa(arpaFormAddress: string): Address4;

245

246

// IPv6 static creation methods

247

static fromBigInt(bigInt: bigint): Address6;

248

static fromByteArray(bytes: Array<any>): Address6;

249

static fromUnsignedByteArray(bytes: Array<any>): Address6;

250

static fromArpa(arpaFormAddress: string): Address6;

251

static fromAddress4(address: string): Address6;

252

```

253

254

**Cross-Format Creation Examples:**

255

256

```typescript

257

// IPv4 from various formats

258

const fromHex = Address4.fromHex("c0a80164");

259

const fromInt = Address4.fromInteger(3232235876);

260

const fromBigInt = Address4.fromBigInt(3232235876n);

261

const fromArpa = Address4.fromArpa("100.1.168.192.in-addr.arpa.");

262

263

console.log(fromHex.correctForm()); // "192.168.1.100"

264

console.log(fromInt.correctForm()); // "192.168.1.100"

265

console.log(fromBigInt.correctForm()); // "192.168.1.100"

266

console.log(fromArpa.correctForm()); // "192.168.1.100"

267

268

// IPv6 from various formats

269

const bigIntV6 = Address6.fromBigInt(BigInt("42540766411282592856903984951653826561"));

270

console.log(bigIntV6.correctForm()); // "2001:db8::1"

271

272

const fromBytes = Address6.fromByteArray([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);

273

console.log(fromBytes.correctForm()); // "2001:db8::1"

274

275

const fromV4 = Address6.fromAddress4("192.168.1.100");

276

console.log(fromV4.correctForm()); // "::ffff:c0a8:164"

277

278

const fromArpaV6 = Address6.fromArpa("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.");

279

console.log(fromArpaV6.correctForm()); // "2001:db8::1"

280

```

281

282

### Format Validation and Correctness

283

284

Check if addresses are in their canonical or correct forms.

285

286

```typescript { .api }

287

/**

288

* Check if address is in correct form

289

* Available on both Address4 and Address6 instances

290

* @returns true if address matches its canonical representation

291

*/

292

isCorrect(): boolean;

293

294

/**

295

* Check if IPv6 address is in canonical form (IPv6 only)

296

* Available on Address6 instances only

297

* @returns true if address is in full canonical form

298

*/

299

isCanonical(): boolean;

300

```

301

302

**Form Validation Examples:**

303

304

```typescript

305

// IPv4 form validation

306

const v4Correct = new Address4("192.168.1.1");

307

const v4Incorrect = new Address4("192.168.001.001");

308

309

console.log(v4Correct.isCorrect()); // true

310

console.log(v4Incorrect.isCorrect()); // false (leading zeros)

311

console.log(v4Incorrect.correctForm()); // "192.168.1.1"

312

313

// IPv6 form validation

314

const v6Compressed = new Address6("2001:db8::1");

315

const v6Canonical = new Address6("2001:0db8:0000:0000:0000:0000:0000:0001");

316

const v6Incorrect = new Address6("2001:0db8::0001");

317

318

console.log(v6Compressed.isCorrect()); // true (properly compressed)

319

console.log(v6Compressed.isCanonical()); // false (not full form)

320

321

console.log(v6Canonical.isCorrect()); // false (should be compressed)

322

console.log(v6Canonical.isCanonical()); // true (full canonical form)

323

324

console.log(v6Incorrect.isCorrect()); // false (unnecessary leading zeros)

325

console.log(v6Incorrect.correctForm()); // "2001:db8::1"

326

```

327

328

### Practical Format Conversion Examples

329

330

Common use cases for format conversions in networking applications.

331

332

**Database Storage:**

333

334

```typescript

335

// Store addresses in different formats for different use cases

336

function storeAddress(addrStr: string) {

337

try {

338

const addr = new Address6(addrStr);

339

return {

340

canonical: addr.canonicalForm(), // For exact matching

341

compressed: addr.correctForm(), // For display

342

binary: addr.binaryZeroPad(), // For bitwise operations

343

bigint: addr.bigInt().toString(), // For numeric operations

344

bytes: addr.toByteArray(), // For binary protocols

345

reverse: addr.reverseForm() // For PTR records

346

};

347

} catch (e) {

348

const addr = new Address4(addrStr);

349

return {

350

canonical: addr.correctForm(),

351

binary: addr.binaryZeroPad(),

352

bigint: addr.bigInt().toString(),

353

bytes: addr.toArray(),

354

reverse: addr.reverseForm()

355

};

356

}

357

}

358

```

359

360

**Network Configuration:**

361

362

```typescript

363

// Generate configuration formats

364

function generateConfig(networkStr: string) {

365

const network = new Address4(networkStr);

366

367

return {

368

cisco: `ip route ${network.correctForm()} ${network.subnetMask}`,

369

binary: network.binaryZeroPad(),

370

hex: network.toHex(),

371

reverseZone: network.reverseForm({ omitSuffix: true })

372

};

373

}

374

375

const config = generateConfig("192.168.1.0/24");

376

console.log(config);

377

// {

378

// cisco: "ip route 192.168.1.0 24",

379

// binary: "11000000101010000000000100000000",

380

// hex: "c0:a8:01:00",

381

// reverseZone: "1.168.192"

382

// }

383

```

384

385

### Types

386

387

```typescript { .api }

388

interface ReverseFormOptions {

389

omitSuffix?: boolean;

390

}

391

```