forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitConverter.hpp
More file actions
90 lines (79 loc) · 2.7 KB
/
BitConverter.hpp
File metadata and controls
90 lines (79 loc) · 2.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* BitConverter.hpp
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __BitConverter_h__
#define __BitConverter_h__
#include "vsqglobal.hpp"
#include <vector>
#include <stdint.h>
VSQ_BEGIN_NAMESPACE
using namespace std;
class BitConverter{
public:
static vector<char> getBytesUInt16BE( uint16_t value ){
vector<char> result;
result.push_back( ((0xff00 & value) >> 8) & 0xff );
result.push_back( ((0x00ff & value)) & 0xff );
return result;
}
static vector<char> getBytesUInt32BE( uint32_t value ){
vector<char> result;
result.push_back( ((0xff000000 & value) >> 24) & 0xff );
result.push_back( ((0x00ff0000 & value) >> 16) & 0xff );
result.push_back( ((0x0000ff00 & value) >> 8) & 0xff );
result.push_back( ((0x000000ff & value)) & 0xff );
return result;
}
/**
* @brief 4 バイトのデータを little endian とみなし、uint32 の整数値を取得する
* @param bytes 変換元のデータ
* @return 変換後の整数値
*/
static uint32_t makeUInt32LE( char bytes[4] ){
return (0xff000000 & (bytes[3] << 24)) |
(0x00ff0000 & (bytes[2] << 16)) |
(0x0000ff00 & (bytes[1] << 8)) |
(0x000000ff & bytes[0]);
}
/**
* @brief 4 バイトのデータを big endian とみなし、uint32 の整数値を取得する
* @param bytes 変換元のデータ
* @return 変換後の整数値
*/
static uint32_t makeUInt32BE( char bytes[4] ){
return (0xff000000 & (bytes[0] << 24)) |
(0x00ff0000 & (bytes[1] << 16)) |
(0x0000ff00 & (bytes[2] << 8)) |
(0x000000ff & bytes[3]);
}
/**
* @brief 2 バイトのデータを little endian とみなし、int16 の整数値を取得する
* @param bytes 変換元のデータ
* @return 変換後の整数値
*/
static int16_t makeInt16LE( char bytes[2] ){
return (0xff00 & (bytes[1] << 8)) |
(0x00ff & bytes[0]);
}
/**
* @brief 2 バイトのデータを big endian とみなし、uint16 の整数値を取得する
* @param bytes 変換元のデータ
* @return 変換後の整数値
*/
static uint16_t makeUInt16BE( char bytes[2] ){
return (0xff00 & (bytes[0] << 8)) |
(0x00ff & bytes[1]);
}
};
VSQ_END_NAMESPACE
#endif