04. Typecasting¶
What is Typecasting?¶
Typecasting means converting one data type into another.
In cybersecurity scripting, typecasting is very important because:
-
Network data often comes in as strings (IP addresses, ports)
-
We need to convert them into integers or bytes for analysis or packet crafting
-
Log files and tool outputs may need conversion before parsing
Types of Typecasting¶
1. Implicit Typecasting (Type Promotion)¶
Python automatically converts smaller data types into larger ones (no data loss).
port = 80 # int
timeout = 0.5 # float
result = port + timeout # int automatically becomes float
print(result, type(result)) # 80.5 <class 'float'>
Useful when mixing numeric types (e.g., timeout calculations for network scans).
2. Explicit Typecasting (Manual Conversion)¶
Done using built-in functions:
-
int()→ convert to integer -
float()→ convert to float -
str()→ convert to string -
list(), tuple(), dict(), set()→ convert between collections -
bytes(), bytearray()→ convert to byte format (important in packet crafting)
Cybersecurity Examples¶
1. Converting Port Numbers (str → int)¶
When reading ports from a file or user input, they are strings by default.
port_str = "443" # read from input or file
port = int(port_str) # convert to integer
print(port, type(port)) # 443 <class 'int'>
Necessary for socket connections.
2. IP Address Handling (str → bytes)¶
For packet crafting with scapy or low-level socket programming, IPs often need to be in bytes.
import socket
ip = "192.168.1.10"
ip_bytes = socket.inet_aton(ip) # Convert to bytes
print(ip_bytes) # b'\xc0\xa8\x01\n'
Useful when building raw packets.
3. Log Parsing (str → int)¶
Security logs may store ports, status codes, or packet counts as strings.
log_entry = "Connection attempt on port 22"
port = int(log_entry.split()[-1]) # "22" -> 22
if port == 22:
print("Possible SSH brute force detected!")
Helps in building intrusion detection scripts.
4. Converting Bytes to String (bytes → str)¶
When capturing traffic or reading files, data often comes as bytes.
Useful for parsing HTTP requests, DNS payloads, etc.
5. Encoding Strings (str → bytes)¶
Passwords, payloads, and requests may need encoding before sending.
payload = "admin:password123"
encoded = payload.encode() # str → bytes
print(encoded) # b'admin:password123'
Required for Base64 encoding, hashing, and network transfers.
6. Hex Encoding / Decoding¶
Cybersecurity tools often use hexadecimal representations.
data = 12345
hex_data = hex(data) # int → hex string
print(hex_data) # '0x3039'
restored = int(hex_data, 16) # hex string → int
print(restored) # 12345
Common in malware analysis & packet inspection.
Quick Reference Table¶
| Conversion | Example | Use in Cybersecurity |
|---|---|---|
int("80") |
str → int | Port numbers from input/logs |
str(443) |
int → str | Printing ports, writing to logs |
bytes("data", "utf-8") |
str → bytes | Sending payloads, encryption |
"text".encode() |
str → bytes | Hashing, network transfer |
b"data".decode() |
bytes → str | Reading packet/log data |
hex(255) |
int → hex | Malware, binary analysis |
int("0xff", 16) |
hex str → int | Packet decoding |
Key Takeaways¶
-
Typecasting is essential in network programming, packet crafting, log analysis, and crypto operations.
-
Always validate input before casting to avoid errors (
ValueErrorif input isn’t numeric). -
Prefer explicit casting in cybersecurity scripts for reliability and clarity.
Reminder: Scripts should only be run on systems you own or have permission to test. Misuse is illegal.