Skip to main content
Home
for DLMS smart meters
Open source solutions for DLMS smart metering

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Forums
  3. How To Convert Byte Array To CGXDLMSVariant?

How to convert Byte Array to CGXDLMSVariant?

Forum Rules

Before commenting read Forum rules

Don't comment the topic if you have a new question.

You can create a new topic selecting correct category from Gurux Forum and then create a new topic selecting "New Topic" from the top left.

Profile picture for user Dmitry
By Dmitry , 23 November, 2021
Forums
Gurux.DLMS

Hi, Mikko!

I have QByteArray with data. I know type of this data. I need to convert it to CGXDLMSVariant.
I try:
uint16_t intVar = 333;
int len_intVar = sizeof(intVar);
QByteArray a;
a.resize(len_intVar);
memcpy(a.data(), &intVar, len_intVar); //convert to QByteArray
-------
// from QByteArray
CGXByteBuffer b;
b.Set(a.data(), a.size());
CGXDLMSVariant v(b);
v.ChangeType(DLMS_DATA_TYPE_UINT16);
But v is not equal 333.

Also i tried:
CGXDLMSVariant v1((unsigned char*)a.data(), a.size(), DLMS_DATA_TYPE_UINT16);
but failed. v1 is not 333.

Could you please write an example?
Thanks!

Profile picture for user Kurumi

Kurumi

4 years 6 months ago

Hi,

Hi,

Do like this if there is no data type in the byte array and it's just raw data.

CGXByteBuffer bb;
bb.SetInt16(333);
CGXDLMSVariant v;
CGXDLMSClient::ChangeType(bb, DLMS_DATA_TYPE_UINT16, v);

BR,
Mikko

Profile picture for user Dmitry

Dmitry

4 years 1 month ago

Hi, Mikko!

Hi, Mikko!
I try:
const std::string obis("0000636207ff");
CGXByteBuffer bb;
bb.SetHexString(obis);
CGXDLMSVariant v;
CGXDLMSClient::ChangeType(bb, DLMS_DATA_TYPE_OCTET_STRING, v);
qDebug() << "v:" << v.ToString().c_str();
And get output: v: 0.0.99.98.7.255. //type: DLMS_DATA_TYPE_STRING but i pass to method DLMS_DATA_TYPE_OCTET_STRING.
I need v as DLMS_DATA_TYPE_OCTET_STRING 0000636207ff which size = 6 bytes. What i'm doing wrong?

Thanks!

Profile picture for user Dmitry

Dmitry

4 years 1 month ago

This code works correctly:

This code works correctly:
QByteArray arr(QByteArray::fromHex(obis.c_str()));
CGXByteBuffer bb1;
bb1.Set(arr.data(), arr.size());
CGXDLMSVariant v1(bb1);
qDebug() << "v1:" << v1.ToString().c_str() << "type:" << v1.vt;

CGXByteBuffer bb2;
v1.GetBytes(bb2);
auto ss=bb2.ToHexString(false);
qDebug() << "ss:" << ss.c_str();

Output:
v1: 00 00 63 62 07 FF type: 9 // 9 - DLMS_DATA_TYPE_OCTET_STRING
ss: 0000636207FF

Profile picture for user Kurumi

Kurumi

4 years 1 month ago

Hi Dmitry,

Hi Dmitry,

The second topic works correctly because v1.ToString converts a byte array to hex string.

If you want to convert a logical name to a string, I propose that you use GXHelpers::GetLogicalName-method for that.

BR,
Mikko

Profile picture for user Dmitry

Dmitry

4 years 1 month ago

Thanks for you reply. Data

Thanks for you reply. Data may be anything. Not just logical name.

Profile picture for user Kurumi

Kurumi

4 years 1 month ago

Hi,

Hi,

Do you want to convert it to the string or hex string?

BR,
Mikko

Profile picture for user Dmitry

Dmitry

4 years 1 month ago

I would like this:

I would like this:
input:
std::string hex_str("0000636207ff");
output:
CGXDLMSVariant v;
v.vt == DLMS_DATA_TYPE_OCTET_STRING;
v.byteArr contains {00, 00, 63, 62, 07, ff}
v.GetSize() == 6

I write function for this:
CGXDLMSVariant hex_string_to_octet_string(std::string_view s)
{
const QByteArray arr(QByteArray::fromHex(s.data()));
CGXByteBuffer bb;
bb.Set(arr.data(), static_cast<unsigned long>(arr.size()));
CGXDLMSVariant v(bb);
return v;
}

but i think it may be done with Gurux.cpp.

Profile picture for user Kurumi

Kurumi

4 years 1 month ago

Hi,

Hi,

You can do it like this:
CGXByteBuffer bb;
bb.SetHexString("0000636207ff");
CGXDLMSVariant v1(bb);

BR,
Mikko

  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Tue, 06/09/2026 - 11:16
    gurux.dlms.java 4.0.95
  • Tue, 06/09/2026 - 10:03
    Gurux.DLMS.Python 1.0.199
  • Mon, 06/08/2026 - 13:39
    gurux.dlms.cpp 9.0.2606.0801
  • Mon, 06/01/2026 - 10:15
    gurux.dlms.cpp 9.0.2606.0101
  • Thu, 05/28/2026 - 16:06
    gurux.dlms.java 4.0.94

New forum topics

  • Error reading L&G Meter
  • Pass a TCP Client to GXNet
  • Australian EDMI Mk10D (Essential Energy area)
  • Strange mix of data notificiation vs get response
  • DLMS Connection
More

Who's new

  • Tuanhgg
  • Adel
  • charnon
  • Paddles
  • Miguel Ángel
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin