Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
int128/example/byte_conversions.cpp at develop · boostorg/int128 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
int128
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
19
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
int128
/
example
/
byte_conversions.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
88 lines (67 loc) · 3.5 KB
develop
Breadcrumbs
int128
/
example
/
byte_conversions.cpp
Copy path
Top
File metadata and controls
Code
Blame
88 lines (67 loc) · 3.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Copyright 2026 Matt Borland
//
Distributed under the Boost Software License, Version 1.0.
//
https://www.boost.org/LICENSE_1_0.txt
#
include
<
boost/int128/int128.hpp
>
#
include
<
boost/int128/byte_conversions.hpp
>
#
include
<
boost/int128/iostream.hpp
>
#
include
<
array
>
#
include
<
cstddef
>
#
include
<
cstdint
>
#
include
<
iomanip
>
#
include
<
iostream
>
//
Prints the bytes of an array in the order they are stored
template
<
typename
Bytes>
void
print_bytes
(
const
char
* label,
const
Bytes& bytes)
{
std::cout << label;
for
(
const
auto
byte : bytes)
{
std::cout <<
'
'
<< std::hex <<
std::setfill
(
'
0
'
) <<
std::setw
(
2
) <<
static_cast
<
unsigned
>(byte);
}
std::cout << std::dec << std::endl;
}
int
main
()
{
using
boost::int128::uint128;
using
boost::int128::int128;
//
The 16 bytes 01 02 ... 10 read as a big-endian value
constexpr
uint128 value {
UINT64_C
(
0x0102030405060708
),
UINT64_C
(
0x090A0B0C0D0E0F10
)};
std::cout <<
"
=== Byte arrays ===
"
<< std::endl;
//
The byte order of the array is the requested one on every platform
print_bytes
(
"
to_be_bytes:
"
,
boost::int128::to_be_bytes
(value));
print_bytes
(
"
to_le_bytes:
"
,
boost::int128::to_le_bytes
(value));
//
Native order is whichever of the two matches the host, so this is the
//
one form whose output depends on the platform
print_bytes
(
"
to_ne_bytes:
"
,
boost::int128::to_ne_bytes
(value));
std::cout <<
"
\n
=== Reading a value back out of bytes ===
"
<< std::endl;
constexpr
std::array<std::
uint8_t
,
sizeof
(uint128)> wire
{{
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x01
,
0x2C
}};
//
The target type is given explicitly, and the byte count has to match
std::cout <<
"
from_be_bytes:
"
<< boost::int128::from_be_bytes<uint128>(wire) << std::endl;
std::cout <<
"
from_le_bytes:
"
<< boost::int128::from_le_bytes<uint128>(wire) << std::endl;
//
Everything is constexpr, so a whole round-trip can be checked at compile time
static_assert
(boost::int128::from_be_bytes<uint128>(
boost::int128::to_be_bytes
(value)) == value,
"
Round trip through big-endian bytes
"
);
std::cout <<
"
\n
=== Signed values ===
"
<< std::endl;
//
The two's complement bit pattern is what gets reversed, so negative
//
values need no special handling
constexpr
int128 negative {-
300
};
print_bytes
(
"
to_be_bytes(-300):
"
,
boost::int128::to_be_bytes
(negative));
std::cout <<
"
from_be_bytes:
"
<< boost::int128::from_be_bytes<int128>(
boost::int128::to_be_bytes
(negative)) << std::endl;
std::cout <<
"
\n
=== Whole value conversions ===
"
<< std::endl;
//
to_be and to_le produce a value whose object representation is in the
//
requested order, which is what a memcpy into a packet buffer wants.
//
The value itself is only meaningful again after the matching from_be / from_le.
const
auto
big_endian_image {
boost::int128::to_be
(value)};
print_bytes
(
"
object representation of to_be(value):
"
,
boost::int128::to_ne_bytes
(big_endian_image));
std::cout <<
"
from_be recovers:
"
<<
boost::int128::from_be
(big_endian_image) << std::endl;
std::cout <<
"
value:
"
<< value << std::endl;
//
Any byte-like element type can be requested, which is convenient when the
//
surrounding buffer is not made of std::uint8_t
const
auto
as_char {boost::int128::to_le_bytes<
char
>(value)};
std::cout <<
"
\n
from_le_bytes over a char buffer:
"
<< boost::int128::from_le_bytes<uint128>(as_char.
data
()) << std::endl;
return
0
;
}
You can’t perform that action at this time.