3 Replies - 1389 Views - Last Post: 21 November 2012 - 06:11 AM Rate Topic: -----

#1 WotCGM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 12

Extracting a value from a binary storage

Posted 16 November 2012 - 02:11 PM

I have forgotten how to manipulate binary #s. Was taught in Pascal 20+ yrs ago.

Issue: I have a field in mysql that is binary and I want to extract the various values
within it based upon their combined values.

Factors:
1. There is an array of items that are coded to an integer.
2. To save space, instead of a mysql field for each, I am using a binary #.
3. Ex: 1 = Tech1, 2= Tech2, 4= Tech3, 8= Tech4 etc...

So.. Instead of having 20 or so fields for each record, I save them as a binary #
such that if the User has Tech1 and 2, Tech1 =1 add Tech2 = 2 == (1 + 2) 3
such that if the User has Tech2 and 3, Tech2 =2 amd Tech3 = 4 == (2 + 4) 6.

I want to write in php or java a function to extract the #s so when I do the printout,
instead of 3, it would print Tech1 and Tech2.

Examples of #s
1 = (1) = Tech1
2 = (2) = Tech2
3 = (1+2) = Tech1 and Tech2
4 = (4) = Tech3
5 = (4 + 1) = Tech3 and Tech1
6 = (4 + 2) = Tech3 and Tech2
7 = (4 + 2 + 1) = Tech3, Tech2, and Tech1
...

Mysql table has (2) fields:
-- Units (table)
ID - type binary 1,2,4,8, etc
Name - type varchar(30)

In the MainUsers table, I would store them as:
-- MainUsers (table)
ID - type binary
UID - type integer (index and autoincre) ID for the actual User
Name - type varchar(30) holds the name of the User.
...
Instead of having indiv. fields for each Unit.

The MainUsers would look like this
UID     ID     Name     ...
1       1      User1    
2       15     User2
3       12     User3

while the Units would look like this
ID     Name
1      Tech1
2      Tech2
4      Tech3
8      Tech4
16     Tech5
32     Tech6
54     Tech7
128    Tech8
256    Tech9
512    Tech10
...


In the above,
User 1 would have Tech1
User 2 would have Techs 1-4
User 3 would have Techs 3-4

I can connect to the db and pull the fields into a php array. I can also update it to add a Tech
to the User (one they do not have yet) w/Update. But am lost from there.

Don't need to "select" what we want (ie do we want a paticular or not),
just print all the info. --- SELECT * from Units

Any help?

Larry

Is This A Good Question/Topic? 0
  • +

Replies To: Extracting a value from a binary storage

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,029
  • Joined: 08-August 08

Re: Extracting a value from a binary storage

Posted 17 November 2012 - 02:04 PM

You want to use bitwise operators:
http://php.net/manua...ors.bitwise.php
If you only had 8 techs you could shift the binary number one position left, see if it is greater than 255 (if it is then tech 8 is true), AND it with 255, then repeat for tech 7 and so on...

This post has been edited by CTphpnwb: 17 November 2012 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 WotCGM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 12

Re: Extracting a value from a binary storage

Posted 19 November 2012 - 03:11 PM

Will have more that 8, but.... that was the answer I needed.
Could not remember "bit-wise".

Went to the link and working with it now.

Thanks!!!

Respects...

Larry
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3489
  • View blog
  • Posts: 10,057
  • Joined: 08-June 10

Re: Extracting a value from a binary storage

Posted 21 November 2012 - 06:11 AM

just pay attention that the number type you use is large enough. (OK, with 220 a 32 bit INT would suffice)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1