KlvLib  1.58
Using KlvLib (read bin data from file) sample
// Copyright IMPLEOTV SYSTEMS LTD. ALL RIGHTS RESERVED
// KlvTestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
#include <iostream>
#include "IKlvLib.h"
#include "Windows.h"
#include <fstream>
#include <string>
using namespace std;
const char LDS_KEY[16] = { 0x06, 0x0E, 0x2B, 0x34, 0x02, 0x0B, 0x01, 0x01, 0x0E, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00 };
const char UDS_KEY[16] = { 0x06, 0x0E, 0x2B, 0x34, 0x02, 0x01, 0x01, 0x01, 0x0E, 0x01, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00 };
char* ReadBinData(const char* path, int& l);
int _tmain(int argc, _TCHAR* argv[])
{
try
{
cout << "Please enter bin file path:" << endl;
string path;
getline (cin, path);
// Read bin data and parse it
int l;
char* buf = ReadBinData(path.c_str(), l);
// 1. Create STANAG 4609 KLV decoder
IKlvDecoder* klvDecoder = CreateKlvDecoder();
// Find out if it is LDS or UDS by comparing to the standard keys.
if ( memcmp(buf, LDS_KEY, IKlvItem::SIXTEEN_BYTES) == 0 )
keyLength = IKlvItem::ONE_BYTE;
else
if ( memcmp(buf, UDS_KEY, IKlvItem::SIXTEEN_BYTES) == 0 )
else
{
cout << "Unknown designator key";
return 0;
}
// 2. Parse the buffer that contains Klv encoded data
if( klvDecoder->Parse(IKlvItem::SIXTEEN_BYTES, keyLength, buf, l ))
{
int itemCount;
IKlvItem** itemArray = klvDecoder->GetItemList(itemCount);
char valueStr[128000];
for(int i = 0; i < itemCount; i++)
{
const char* keyStr = itemArray[i]->GetKeyString();
int length = itemArray[i]->GetLength();
unsigned char* value = (unsigned char* )itemArray[i]->GetValue();
for(int i=0; i<length; i++)
{
sprintf(valueStr + i*2, "%02X", *(value+i));
}
cout << keyStr << ": Ox" << valueStr << endl;
}
}
//3. Delete KLV Decoder
delete klvDecoder;
delete[] buf;
}
catch(char * str )
{
cout << "Exception raised: " << str << '\n';
}
getchar();
return 0;
}
char* ReadBinData(const char* path, int& l)
{
char * buffer;
ifstream is;
is.open(path, ios::in|ios::out|ios::binary|ios::ate);
if ( ! is.is_open() )
{
throw "open failed";
}
// get length of file:
is.seekg (0, ios::end);
l = is.tellg();
is.seekg (0, ios::beg);
buffer = new char [l];
is.read (buffer,l);
is.close();
return buffer;
}
Untitled 1




 Copyright 2010,    IMPLEOTV SYSTEMS LTD