I have a Visual Studio 2008 C++ project where I need to decode a 6-byte array of data in to a structure.
My thought was to do something like this:
#pragma pack( push, 1 )
struct Map
{
BYTE alpha : 8;
BYTE bravo : 8;
UINT charlie : 24;
BYTE delta : 8;
BYTE padding0 : 8; // ensure correct byte boundary
BYTE padding1 : 8;
};
#pragma pack( pop )
// sizeof( Map ) == 8 bytes
BYTE test_data[ 6 ] = { 0x07, // alpha
0x0B, // bravo
0x00, // charlie high byte
0x00, // charlie med byte
0x01, // charlie low byte
0x33 }; // delta
Map map = { };
memcpy( &map, test_data, sizeof( test_data ) );
ASSERT( map.alpha == 0x07 );
ASSERT( map.bravo == 0x0B );
ASSERT( map.charlie == 0x01 );
ASSERT( map.delta == 0x33 );
But, the data in charlie and delta is never correct.
Is there a way to structure my struct to get this correct or am I stuck setting each field individually?
Thanks
To do this in a portable way:
struct Map
{
BYTE alpha;
BYTE bravo;
UINT charlie;
BYTE delta;
Map(const BYTE data[6])
: alpha(data[0])
, bravo(data[1])
, charlie((data[2] << 16) | (data[3] << 8) | data[4])
, delta(data[5])
{}
};
int main()
{
BYTE test_data[ 6 ] = { 0x07, // alpha
0x0B, // bravo
0x00, // charlie high byte
0x00, // charlie med byte
0x01, // charlie low byte
0x33 }; // delta
Map map(test_data);
ASSERT( map.alpha == 0x07 );
ASSERT( map.bravo == 0x0B );
ASSERT( map.charlie == 0x01 );
ASSERT( map.delta == 0x33 );
}
You cannot serialize compound types naively. Just write it yourself:
std::tuple<char, char, unsigned int, char>
parse(char const * buf)
{
return std::make_tuple(buf[0],
buf[1],
buf[2] + 256 * buf[3] + 256 * 256 * buf[4],
buf[5]);
}
Of course you have to match the endianness of the integer to that which is documented by your serialized format, little-endian in my example.
Related
I have a file 'data.dat' with 26 hex byte values:
22 49 E1 09 62 18 42 8C 66 10 B0 11 84 9C 00 FF E0 40 1F F8 60 07 FE 2C 03 FF
I am trying to read these into c++ and print the hex values to the terminal with:
const int msgbuflen = 26;
char message [msgbuflen];
const char* filenameIn = "data.dat";
FILE* fpIn = fopen(filenameIn, "rb");
if (!fpIn) {
perror("ERROR: INPUT FILE CANNOT BE OPENED\n");
exit(EXIT_FAILURE);
}
for (i=0; i<msgbuflen; i++){ // clear the buffer
message[i] = '0';
}
size_t ret_code = fread(message, sizeof(unsigned char), msgbuflen, fpIn);
for(int i = 0; i < msgbuflen; i++){
printf("message[%d] : 0x%x\n", i, message[i]);
}
fclose (fpIn);
When run the output for some of the bytes have 3 leading ff values:
message[0] : 0x22
message[1] : 0x49
message[2] : 0xffffffe1
message[3] : 0x9
message[4] : 0x62
message[5] : 0x18
message[6] : 0x42
message[7] : 0xffffff8c
message[8] : 0x66
message[9] : 0x10
message[10] : 0xffffffb0
message[11] : 0x11
message[12] : 0xffffff84
message[13] : 0xffffff9c
message[14] : 0x0
message[15] : 0xffffffff
message[16] : 0xffffffe0
message[17] : 0x40
message[18] : 0x1f
message[19] : 0xfffffff8
message[20] : 0x60
message[21] : 0x7
message[22] : 0xfffffffe
message[23] : 0x2c
message[24] : 0x3
message[25] : 0xffffffff
Why do these leading f's occur? for example message[2] : 0xffffffe1
I have tried formatting the output of the printf hex %x with 0x%01x but it makes no difference to the terminal output. Checking the sizeof each element in the char array, they are still 1 byte as expected:
printf("sizeof(message[2]) : %ld\n", sizeof(message[2]) );
%> sizeof(message[2]) : 1
I am now wondering if this is a formatting problem? There does not appear to be any more than 1 byte in each message element (as expected).
Using std::hex with cout produces the same issue.
This is because char is a signed integer on your machine, so 0xFF becomes -1. As %x prints an int, which is 32-bits, it gets sign-extended to 0xFFFFFFFF which is a 32-bit -1.
If you store as an unsigned char, you will not have this problem:
#include <cstdio>
int main() {
unsigned char message[] = {0x22, 0x49, 0xE1, 0x09, 0x62, 0x18, 0x42, 0x8C, 0x66, 0x10, 0xB0, 0x11, 0x84, 0x9C, 0x00, 0xFF, 0xE0, 0x40, 0x1F, 0xF8, 0x60, 0x07, 0xFE, 0x2C, 0x03, 0xFF };
int msgbuflen = sizeof(message)/sizeof(message[0]);
for(int i = 0; i < msgbuflen; i++){
printf("message[%d] : 0x%x\n", i, message[i]);
}
}
Produces:
Program returned: 0
message[0] : 0x22
message[1] : 0x49
message[2] : 0xe1
message[3] : 0x9
message[4] : 0x62
message[5] : 0x18
message[6] : 0x42
message[7] : 0x8c
message[8] : 0x66
message[9] : 0x10
message[10] : 0xb0
message[11] : 0x11
message[12] : 0x84
message[13] : 0x9c
message[14] : 0x0
message[15] : 0xff
message[16] : 0xe0
message[17] : 0x40
message[18] : 0x1f
message[19] : 0xf8
message[20] : 0x60
message[21] : 0x7
message[22] : 0xfe
message[23] : 0x2c
message[24] : 0x3
message[25] : 0xff
Godbolt: https://godbolt.org/z/oxPqnYqMq
Alternatively, you can just cast each char to unsigned char:
for(int i = 0; i < msgbuflen; i++){
printf("message[%d] : 0x%x\n", i, (unsigned char)message[i]);
}
Or in somewhat more current C++ (not using "C" style arrays) and a well defined unsigned 8 bits datatype. (char can be either signed or unsigned depending on the platform you are working on)
#include <array>
#include <iostream>
#include <sstream>
#include <cstdint>
#include <format>
// simulated opening of a std::ifstream
auto open_file()
{
static std::array<std::uint8_t,26> data
{
0x22, 0x49, 0xE1, 0x09, 0x62, 0x18, 0x42, 0x8C, 0x66, 0x10,
0xB0, 0x11, 0x84, 0x9C, 0x00, 0xFF, 0xE0, 0x40, 0x1F, 0xF8,
0x60, 0x07, 0xFE, 0x2C, 0x03, 0xFF
};
std::istringstream is(std::string{ data.begin(), data.end() });
return is;
}
int main()
{
std::array<std::uint8_t, 26> buffer;
auto is = open_file();
// get doesn't have an overload for std::uint8_t*
// so check if sizeof char matches sizeof std::uint8_t
static_assert(sizeof(char) == sizeof(std::uint8_t));
is.get(reinterpret_cast<char*>(buffer.data()), buffer.size());
for (const auto byte : buffer)
{
std::cout << std::format("0x{:x} ", byte);
}
return 0;
}
I'm trying to cast a 115bit data to a union of bitfields and getting a wrong result.
Setup
I have two types of data:
Configuration: data[62:0]
addr[113:107]
type[114]
RawBits: lowBits[63:0]
highBits[115:64]
So I defined the following bitfields and union:
typedef struct RawBits {
unsigned long int lowBits:64;
unsigned long int highBits:51;
unsigned long int reserved :13;
} __attribute__((packed))rawBits;
typedef struct Configuration {
unsigned long int data : 62;
unsigned long int addr : 7;
unsigned long int type : 1;
unsigned long int reserved : 58;
} __attribute__((packed))Configuration ;
typedef union Instruction {
RawBits bits;
Configuration configuration;
} __attribute__((packed))Instruction;
As data I used:
uint8_t configuration_test[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00,
And to convert the buffer to the union type I used simple casting:
Instruction *instruction = (Instruction *)configuration_test;
Expected result
instruction->bits->lowBits = 0xfffffffffffffffc
instruction->bits->highBits = 0x00000000000001fc
instruction->bits->reserved = 0x0000000000000000
instruction->configuration->data = 0x3fffffffffffffff
instruction->configuration->addr = 0x000000000000007f
instruction->configuration->type = 0x0000000000000000
instruction->configuration->reserved = 0x0000000000000000
Real result
instruction->bits->lowBits = 0xfcffffffffffffff
instruction->bits->highBits = 0x0004010000000000
instruction->bits->reserved = 0x000000000000001f
instruction->configuration->data = 0x3cffffffffffffff
instruction->configuration->addr = 0x0000000000000003
instruction->configuration->type = 0x0000000000000000
instruction->configuration->reserved = 0x0003f00400000000
we're making keyboard with five sensor and we have problem with mojibake
if we click sensor 'a' five time, there should be five 'ㄱ'
but there are mojibake like attached picture.
we think this problem is related with unicode but can't find what the exact probelm is
please answer what is problem. thank you
#include <SoftwareSerial.h>
#include<Wire.h>
#define BT_RXD 8
#define BT_TXD 7
SoftwareSerial bluetooth(BT_RXD, BT_TXD);
int SensorPin1 = A0;
int SensorPin2 = A1;
int SensorPin3 = A2;
int SensorPin4 = A3;
int SensorPin5 = A4;
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
int SensorReading1 = analogRead(SensorPin1);
int SensorReading2 = analogRead(SensorPin2);
int SensorReading3 = analogRead(SensorPin3);
int SensorReading4 = analogRead(SensorPin4);
int SensorReading5 = analogRead(SensorPin5);
int FSR1 = map(SensorReading1, 0, 1024, 0, 255);
int FSR2 = map(SensorReading2, 0, 1024, 0, 255);
int FSR3 = map(SensorReading3, 0, 1024, 0, 255);
int FSR4 = map(SensorReading4, 0, 1024, 0, 255);
int FSR5 = map(SensorReading5, 0, 1024, 0, 255);
Wire.beginTransmission(MPU_addr); //-32768~32767
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read() << 8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read() << 8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read() << 8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read() << 8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read() << 8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read() << 8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read() << 8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.println(AcZ);
//Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
//Serial.print(" | GyX = "); Serial.print(GyX);
//Serial.print(" | GyY = "); Serial.print(GyY);
//Serial.print(" | GyZ = "); Serial.println(GyZ);
if(FSR1 > 100)
{
//Serial.println("ㄱ");
bluetooth.write("ㄱ");
}
if(FSR2 > 100)
{
//Serial.println("ㄱ");
bluetooth.write(FSR2);
}
if(FSR3 > 100)
{
//Serial.println("ㄱ");
}
if(FSR4 > 100)
{
//Serial.println("2");
}
if(FSR5 > 100)
{
//Serial.println("ㄱ");
bluetooth.write("ㄱ");
}
if (bluetooth.available())
{
//Serial.write(bluetooth.read());
}
if (Serial.available())
{
//bluetooth.write(FSR);
}
delay(333);
}
What you are sending is bytes, and the exact bytes you are sending depends on the encoding of your editor. When your capston application receives those bytes, it tries to interpret them according to the encoding of capston. No idea what it might be. In any case, what you need to do is to send utf-8 data from the arduino, and ensure that the capston application decodes utf-8 data.
I am attempting to send hexadecimal bytes to a serial com port. The issue is that the segment that sends the command apparently wants a system string instead of an integer (error C2664 "cannot convert parameter 1 from 'int' to 'System::String ^'). I have looked for a way to send an integer instead but have had no luck. (I have tried sending string representations of the hexadecimal values, but the device did not recognize the commands)
Main part of Code
private: System::Void poll_Click(System::Object^ sender, System::EventArgs^ e)
{
int i, end;
double a = 1.58730159;
String^ portscan = "port";
String^ translate;
std::string portresponse [65];
std::fill_n(portresponse, 65, "Z");
for (i=1;i<64;i++)
{
if(this->_serialPort->IsOpen)
{
// Command 0 generator
int y = 2;
y += i;
int command0[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, dectohex(i), 0x00, 0x00, dectohex(y)};
for (end=0;end<10;end++)
{
this->_serialPort->WriteLine(command0[end]);
}
translate = (this->_serialPort->ReadLine());
MarshalString(translate, portresponse [i]);
if(portresponse [i] != "Z")
{
comboBox7->Items->Add(i);
}
this->progressBar1->Value=a;
a += 1.58730159;
}
}
}
Here is the function dectohex:
int dectohex(int i)
{
int x = 0;
char hex_array[10];
sprintf (hex_array, "0x%02X", i);
string hex_string(hex_array);
x = atoi(hex_string.c_str());
return x;
}
This is what solved my problem, courtesy of Jochen Kalmbach
auto data = gcnew array<System::Byte> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD };
_serialPort->Write(data, 0, data->Length);
Replaced this
this->_serialPort->WriteLine(command0[end]);
You cannot sent an integer over a serial line.... you can only sent BYTES (7-8 bit)!
You need to choose what you want to do:
Sent characters: So the "number" 12 will be converted into the bytes
_serialPort->Write(12.ToString());
// => 0x49, 0x50
Sent the integer (4 bytes) as little endian
auto data = System::BitConverter::GetBytes(12);
_serialPort->Write(data, 0, data->Length);
// => 0x0c, 0x00, 0x00, 0x00
Or you write just a single byte:
auto data = gcnew array<System::Byte> { 12 };
_serialPort->Write(data, 0, data->Length);
// => 0x0c
Or write an byte array:
auto data = gcnew array<System::Byte> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD };
_serialPort->Write(data, 0, data->Length);
// => 0xFF 0xFF 0xFF 0xFF 0xFF 0x02 0xBF 0x00 0x00 0xBD
I need to get the product version and file version for a DLL or EXE file using Win32 native APIs in C or C++. I'm not looking for the Windows version, but the version numbers that you see by right-clicking on a DLL file, selecting "Properties", then looking at the "Details" tab. This is usually a four-part dotted version number x.x.x.x.
You would use the GetFileVersionInfo API.
See Using Version Information on the MSDN site.
Sample:
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSize( szVersionFile, &verHandle);
if (verSize != NULL)
{
LPSTR verData = new char[verSize];
if (GetFileVersionInfo( szVersionFile, verHandle, verSize, verData))
{
if (VerQueryValue(verData,"\\",(VOID FAR* FAR*)&lpBuffer,&size))
{
if (size)
{
VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
if (verInfo->dwSignature == 0xfeef04bd)
{
// Doesn't matter if you are on 32 bit or 64 bit,
// DWORD is always 32 bits, so first two revision numbers
// come from dwFileVersionMS, last two come from dwFileVersionLS
TRACE( "File Version: %d.%d.%d.%d\n",
( verInfo->dwFileVersionMS >> 16 ) & 0xffff,
( verInfo->dwFileVersionMS >> 0 ) & 0xffff,
( verInfo->dwFileVersionLS >> 16 ) & 0xffff,
( verInfo->dwFileVersionLS >> 0 ) & 0xffff
);
}
}
}
}
delete[] verData;
}
All these solutions did not work properly (with my system). I found out that each of the four parts of the version number are saved as a 16-bit value.
The first two numbers are saved in the 32-bit DWORD dwFileVersionMS, and the second two in dwFileVersionLS. So I edited your code at the output section like this:
TRACE( "File Version: %d.%d.%d.%d\n",
( pFileInfo->dwFileVersionMS >> 16 ) & 0xffff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xffff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xffff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xffff
);
And it works perfectly. The output is formatted like on my system:
major.minor.build.revision
You get this information using the version information APIs. Here is a sample:
void PrintFileVersion( TCHAR *pszFilePath )
{
DWORD dwSize = 0;
BYTE *pbVersionInfo = NULL;
VS_FIXEDFILEINFO *pFileInfo = NULL;
UINT puLenFileInfo = 0;
// Get the version information for the file requested
dwSize = GetFileVersionInfoSize( pszFilePath, NULL );
if ( dwSize == 0 )
{
printf( "Error in GetFileVersionInfoSize: %d\n", GetLastError() );
return;
}
pbVersionInfo = new BYTE[ dwSize ];
if ( !GetFileVersionInfo( pszFilePath, 0, dwSize, pbVersionInfo ) )
{
printf( "Error in GetFileVersionInfo: %d\n", GetLastError() );
delete[] pbVersionInfo;
return;
}
if ( !VerQueryValue( pbVersionInfo, TEXT("\\"), (LPVOID*) &pFileInfo, &puLenFileInfo ) )
{
printf( "Error in VerQueryValue: %d\n", GetLastError() );
delete[] pbVersionInfo;
return;
}
// pFileInfo->dwFileVersionMS is usually zero. However, you should check
// this if your version numbers seem to be wrong
printf( "File Version: %d.%d.%d.%d\n",
( pFileInfo->dwFileVersionLS >> 24 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 8 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xff
);
// pFileInfo->dwProductVersionMS is usually zero. However, you should check
// this if your version numbers seem to be wrong.
printf( "Product Version: %d.%d.%d.%d\n",
( pFileInfo->dwProductVersionLS >> 24 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 16 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 8 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 0 ) & 0xff
);
}
Found these articles...sorry, but I don't have direct experience with how to do this using native APIs, so I deferred to an Internet search:
C++: Determining the version number of a DLL or Executable
VB (probably version 6): How to Use Functions in VERSION.DLL
Hope these help!
This code shows the file version numbers correctly.
( pFileInfo->dwFileVersionMS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xff);
The easiest way is to use the GetFileVersionInfoEx or GetFileVersionInfo API functions.
You can also do it from within your application resources as explained here.