Main Site Documentation

C++ string to byte array


#1

Anyone knows how I can convert a string to a byte array:


const char fezAddr = "RHINO"; 
// fezAddr needs to become:
const uint64_t fezAddress = 0x4F4E494852LL;
// note that the array is reversed (ONIHR)

PS.: Need this on an Arduino


#2

the correct c++ statement should be:

const char* fezAddr ="RHINO";

and, this is already an array.

char c = fezAddr[0];

#3

Mike,

i’m not getting it completely, but thats because i probably didn’t explained it well.

I’m using an arduino library to send data wireless to a FEZ board (works pretty well i must say).
The problem is more cosmetic.

In the arduino library you have to specify the receiver address in the following way:


const uint64_t fezAddress = 0x4F4E494852LL;

The address specified on my FEZ board is “RHINO”. I would like to be able to specify also the address as “RHINO” instead of manually converting each character to hex and reverse it.

So what i’m looking for is a way to reverse “RHINO” into “ONIHR”, convert that to hex and add “0x” and “LL”

Does that make sense?


#4

Not yet! :smiley:

I assume this is a C++ question not c#?

Do big/little endian issues arise also?


#5

Use your uint64 (not const)
Start with the last character, use left shift (8bit) on uint64 and OR it with the character value
Continue to the next character until you are done.


#6

Thats what the topic title says, so yes :wink: