473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

bits manipulation

Hi,

I have a BYTE array and I have "mapped" every bits with a string, a type
and an offest:

For istance my array is 20 bytes long and I did this :

first name :
offset 0 (bits)
length 8*10 (bits) - every charater is coded on 8 bytes
type string

age :
offset 80
length 7 (bits) - I assume we cannot leave more than 128 years
type numeric
and now I would like to do something like :

fstname = GetString("first name");
age = GetNumeric("age");

of
SetData("first name", "john");
SetData("age", 27);

I know how to do the map thing but how can I manipulate easily bits ?


Jul 23 '05 #1
3 1411
What are you trying to do? Save memory? Process faster? Unfortunately
this solution doesnt help in either of the causes.

Please update with a little more context and we'll try to help

Jul 23 '05 #2
Raghu Uppalli a écrit :
What are you trying to do? Save memory? Process faster? Unfortunately
this solution doesnt help in either of the causes.

Please update with a little more context and we'll try to help


I am reading files(BYTE array) on a smart card and I need to associate
to each chunk some data and some type.
For instance for a smart card of type 1 information like name, age and
so on are stored on a certain position and on defined number of bits.
On another smart card, these information are stored with a different
offset. So I need to access data transparently without playing
each time with bits manipulation.

I need to be able to get my data jut by specifying a key (string).
The goal is to specify the smart card mapping in a XML file so that
I don't have to recompile everytyme I have a new type of smart card.
So now with a std::map I associate to a key a tag, an offset, a size and
a length.
So finally I have an array like this
------------------------------
|0|1|2|3|4|5|6|7|8|9|10|11|12|
------------------------------
< name >< age >

So I associate the name tag to offset 0 to 8 and the type is a string
and age to offset 9 to 12 as a numeric.
I need after to be able to do

strname = myarray.GetString("name");
or nAge = myarray.GetNumeric("age");


Hope this helps

Jul 23 '05 #3
Vince schrieb:
I am reading files(BYTE array) on a smart card and I need to associate
to each chunk some data and some type.
The card can't be that smart when it only allows for 10-character names
(or less than that if the name requires a multibyte encoding) - SCNR ;)
But seriously, what would you think if you had a long name and someone
decided it's okay to truncate it for the sake of saving a few bytes? I
think it's insulting.

[rationale snipped]
So finally I have an array like this
------------------------------
|0|1|2|3|4|5|6|7|8|9|10|11|12|
------------------------------
< name >< age >

So I associate the name tag to offset 0 to 8 and the type is a string
and age to offset 9 to 12 as a numeric.
I need after to be able to do

strname = myarray.GetString("name");
or nAge = myarray.GetNumeric("age");


Okay, somehow I feel like giving a complete example :-)

#include <cassert>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

typedef std::vector< unsigned char > card_data;

unsigned char extract( const card_data& data, size_t offset,
size_t bits )
{
assert( data.size() >= ( offset + bits + 7 ) / 8 );

// grab a byte (or less) from an arbitrary bit offset into data
unsigned char result =
( data[ offset / 8 ] << offset % 8 ) & ( 0xff << offset % 8 );
if ( bits > 8 - offset % 8 )
result |= ( ( data[ offset / 8 + 1 ] >> ( 8 - offset % 8 ) )
& ( 0xff >> ( 8 - offset % 8 ) ) );
return result >> ( 8 - bits % 8 ) % 8;
}

std::string get_string( const card_data& data, size_t offset,
size_t length )
{
std::string result;
for ( card_data::size_type i = 0; i < ( length + 7 ) / 8; ++i )
{
if ( char c = extract( data, 8 * i + offset,
std::min( length - 8 * i, 8u ) ) )
result += c;
else break;
}
return result;
}

template< typename T >
T get_numeric( const card_data& data, size_t offset, size_t length )
{
assert( ( length + 7 ) / 8 <= sizeof( T ) );

T result = 0;
// Assuming big-endian bit order.
// Take care to handle other formats properly
for ( size_t i = 0; i < ( length + 7 ) / 8; ++i )
result |= extract( data, 8 * i + offset,
std::min( length - 8 * i, 8u ) )
<< ( length - std::min( 8 * ( i + 1 ), length ) );
// sign-extend if needed
if ( std::numeric_limits< T >::is_signed &&
result & ( 1 << ( length - 1 ) ) )
result |= T( -1 ) << length;
return result;
}

int main()
{
card_data test;
// This was of course was read from the card
test.push_back( 0x25 );
test.push_back( 0x37 );
test.push_back( 0xb4 );
test.push_back( 0x37 );
test.push_back( 0x00 );
test.push_back( 0x7f ); // all-1 padding for no particular reason
test.push_back( 0xff );
test.push_back( 0xff );
test.push_back( 0xff );
test.push_back( 0xff );
test.push_back( 0x9e );
// Look up the offset/length in your card description database
// according to the card type and field name
// For the fun of it, let's not start on a byte boundary
std::cout << get_string( test, 1, 80 ) << " is "
<< get_numeric< unsigned >( test, 81, 7 )
<< " years old" << std::endl;
std::string s;
std::getline( std::cin, s );
}

Cheers,
Malte
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Danny Anderson | last post by:
Hola! I have an array of shorts that represents a stream of data. This data is packed in tight, so certain elements may actually contain data for more than one variable. I am to decipher this...
8
by: vib | last post by:
Hi there, Below is the code that compares bit by bit of 32-bits word. Two errors(bits) are allowed. I am not sure if this is the right way to check for x number of error bits. Advice and...
7
by: Marty McFly | last post by:
Hello VB Gurus, I have an unusual requirement to shift an unsigned int right one bit: Dim myVar As UInt32 = UInt32.Parse("123456") Dim myResult As UInt32 myResult = myVar >> 1 However, the...
32
by: Mark | last post by:
lets say i have a char, and i want to check if the 3rd bit is 0 or 1... how might i do this? thanks!
21
by: felixnielsen | last post by:
If im not mistaken, a char variable allocates 1 byte of memory, as it is the case with a bool variable. I need 2 bool and 1 char variable which only need to contain a value between 0 and 63...
2
by: Harry Strybos | last post by:
Hi All I have a form with about 20 check boxes and I want to persist their state to a single integer. That is to say, I want to set their Checked property to a particular bit value contained in...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
4
by: harryos | last post by:
hi i am reading thru a book on digitizing text lines using c code.They use a font data file containing unsigned char fonts with elements like 0x00,0x7e,0x81 etc to represent each character .After...
2
by: s gall | last post by:
Hi all I have a task to test a checksum algrorithum. This requires changing a string of 12 chr's to a stream of 96 bits and then manipulating these. Currently I work in vb (vb2008). Would it be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.