ByteSwap Literals
ByteSwap Literals is a Visual Studio Code extension for manually byte-swapping integer literals in C and C++ source files. It is designed for the workflow where you are inspecting constants, protocol fields, register values, or embedded data and want to flip the byte order directly in the editor.
Features
- Swap the integer literal under the cursor with the
ByteSwap: Swap Literal Under Cursor command.
- Keep the literal base and suffix when rewriting the value.
- Optionally show both the current and byte-swapped interpretations in the hover tooltip, including decimal, hexadecimal, and binary forms.
- Focus on C and C++ editing scenarios, including
c, cpp, cuda-cpp, objective-c, and objective-cpp language IDs.
Supported literals
The extension currently targets integer literals only.
- Decimal:
4660, 287454020U
- Hexadecimal:
0x1234, 0X12'34ULL
- Binary:
0b00000001'00000010
- Octal:
01234
Digit separators using ' are supported. Floating-point literals are intentionally excluded.
Command
ByteSwap: Swap Literal Under Cursor
Open the Command Palette and run the command while the cursor is on an integer literal.
Keyboard shortcut binding
No default keybinding is shipped. To add one:
- Open Keyboard Shortcuts.
- Search for
ByteSwap: Swap Literal Under Cursor.
- Bind the key combination you want.
You can also bind it directly in keybindings.json:
{
"key": "ctrl+alt+b",
"command": "byteswap.swapLiteral",
"when": "editorTextFocus && editorLangId =~ /^(c|cpp|cuda-cpp|objective-c|objective-cpp)$/"
}
Settings
This extension contributes one setting:
byteswap.hover.enabled: Default false. When enabled, hovering an integer literal shows the current and byte-swapped interpretations in decimal, hexadecimal, and binary.
Hover output
When hover support is enabled, the extension shows:
- The normalized bit width used for the swap
- The current unsigned and signed interpretations
- The byte-swapped unsigned and signed interpretations
- Hex and binary renderings for both current and swapped values
This is intentionally symmetrical because a literal alone does not tell you which endianness is semantically correct.
Development
npm install
npm test
Useful scripts:
npm run compile
npm run watch
npm run test
npm run vsix
Marketplace packaging
The project is configured for MIT licensing and Marketplace publishing under the srejv publisher. Build a package with:
npm run vsix
Publish with your Marketplace credentials using vsce publish or the equivalent CI flow you prefer.