forked from sttp/cppapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndianConverter.cpp
More file actions
75 lines (65 loc) · 2.37 KB
/
EndianConverter.cpp
File metadata and controls
75 lines (65 loc) · 2.37 KB
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
//******************************************************************************************************
// EndianConverter.cpp - Gbtc
//
// Copyright © 2019, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 03/19/2012 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
#include "EndianConverter.h"
using namespace sttp;
// Creates a new instance of the EndianConverter.
EndianConverter::EndianConverter()
{
constexpr union
{
uint32_t num;
uint8_t bytes[4];
}
endianTest = { 0x00000001 };
if (endianTest.bytes[0] == 1)
m_nativeOrder = LittleEndian;
else
m_nativeOrder = BigEndian;
}
// Swaps the bytes in a character array.
// Used for conversion between different byte orders.
void EndianConverter::ByteSwap(uint8_t* value, const uint32_t length)
{
uint8_t *start, *end;
for (start = value, end = value + length - 1; start < end; ++start, --end)
{
const uint8_t temp = *start;
*start = *end;
*end = temp;
}
}
int EndianConverter::NativeOrder() const
{
return m_nativeOrder;
}
EndianConverter EndianConverter::Default;
bool EndianConverter::IsLittleEndian()
{
static const bool isLittleEndian = Default.NativeOrder() == LittleEndian;
return isLittleEndian;
}
bool EndianConverter::IsBigEndian()
{
static const bool isBigEndian = Default.NativeOrder() == BigEndian;
return isBigEndian;
}