Skip to content

Instantly share code, notes, and snippets.

@dniklaus
Last active November 24, 2018 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dniklaus/c0ef09ac4e1730b265d9003e025d3437 to your computer and use it in GitHub Desktop.
Save dniklaus/c0ef09ac4e1730b265d9003e025d3437 to your computer and use it in GitHub Desktop.
LoRaKey string to byte array conversion
//============================================================================
// Name : testLoraKey.cpp
// Author : nid
//============================================================================
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void convertStringToByteArray(unsigned char* byteBuffer, const char* str)
{
unsigned int len = strlen(str);
for (unsigned int i = 0; i < len; i+=2)
{
char substr[3];
const char* strSel = &(str[i]);
strncpy(substr, strSel, 2);
substr[2] = '\0';
byteBuffer[i/2] = static_cast<unsigned int>(strtol(substr, NULL, 16));
}
}
void prettyPrintSessionKey(const char* sessionKeyName, const char* sessionKeyString)
{
unsigned char byteBuffer[16];
convertStringToByteArray(byteBuffer, sessionKeyString);
printf("%s[%u] = {", sessionKeyName, sizeof(byteBuffer));
for (unsigned int i = 0; i < sizeof(byteBuffer); i++)
{
if (i != 0)
{
printf(",");
}
printf("0x%02X", byteBuffer[i]);
}
printf("};\n");
}
int main()
{
const char nwkSKeyStr[] = "0123123D234A12450132413241243155";
// static const PROGMEM u1_t NWKSKEY[16] = {0x01,0x23,0x12,0x3D,0x23,0x4A,0x12,0x45,0x01,0x32,0x41,0x32,0x41,0x24,0x31,0x55};
const char appSKeyStr[] = "0123123D234A12450132413241243156";
// static const u1_t PROGMEM APPSKEY[16] = {0x01,0x23,0x12,0x3D,0x23,0x4A,0x12,0x45,0x01,0x32,0x41,0x32,0x41,0x24,0x31,0x56};
const char devAddrStr[] = "26011BF3";
unsigned int devAddr = static_cast<unsigned int>(strtol(devAddrStr, 0, 16));
prettyPrintSessionKey("nwkSKey", nwkSKeyStr);
prettyPrintSessionKey("appSKey", appSKeyStr);
printf("devAddr = 0x%08X;\n", devAddr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment