[prev in list] [next in list] [prev in thread] [next in thread] 

List:       botan-devel
Subject:    [Botan-devel] Conversion from int to const byte array
From:       lloyd () randombit ! net (Jack Lloyd)
Date:       2005-09-16 1:37:18
Message-ID: 20050916013718.GO1855 () randombit ! net
[Download RAW message or body]


char is easy :)

char c = /*whatever*/;
byte arr[1] = { (byte)c };

For larger integer types, there is a get_byte function in Botan that pulls the
Nth byte out of an integer type. So for example to marshall a unsigned int:

for(size_t j = 0; j != sizeof(unsigned int); j++)
  buf[j] = get_byte(j, my_int);

get_byte is pretty simple, just a shift and mask to pull out the correct bytes,
so you can easily skip the function call and still have things readable.

Strings are somewhat nontrivial, as there are many ways one could encode them
(well, there are many ways to encode integers as well, but as a big-endian byte
stream is by far the most commmon). For example, you could do length+value, or
value+terminator, or any number of other things. There are also issues of
canonicalization with strings - for example, if you have two strings, one with
a umlaut u in UTF-8, and other in Latin-1, are they the same string or not?
Without knowing more I can't provide much useful advice about that.

-Jack

On Wed, Sep 14, 2005 at 06:25:36PM -0400, Carlton Davis wrote:
> I need to sign some fields in a packet header.
> The fields can be int, char or string. Does Botan
> provides any method for converting these data type
> to const byte[]. If not how can I do these conversion?
> I am relatively new to C++, but my application needs to
> be developed in C++.
> 
> Thanks in advance and best regards.
> 
> -Carlton
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic