Solved

MySQL STRING functions for BINARY formatting

Posted on 2011-02-21
4
256 Views
Last Modified: 2012-06-21
I want to display a 32-bit integer as binary, but formatted to have a space every 4 chars.
I assume we start with BIN(FieldName), but what's the best way to insert spaces (or commas) every fourth resulting character?
INSERT?  REGEX?

thanks!
0
Comment
Question by:willsherwood
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34947385
Do you have a programming language to work with, eg PHP? The formatting part is probably more easily done through something other than MySQL
0
 

Author Comment

by:willsherwood
ID: 34947623
yes but this is a "phrase" that goes into an app that generates PHP&MySQL code.
Thus it needs to be encapsulated (albeit lengthy!) for a SELECT query.
i understand PHP would be easier, but really need it in MySQL.
There are brute force strategies to mask&shift for each 4 bits to display, but i was hoping for a clever REGEX or other tricky way :)
I could even do four groups of 8 bits, if the code is really ugly.

thanks
0
 

Author Comment

by:willsherwood
ID: 34947654
... or maybe it's just a series of  
CONCAT(
   LEFT(BIN(FieldName),4),' ',
   MID(BIN(FieldName),5,4),' ',
     ...
   )
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 34990179
I think that's probably the best you can do. Regular expressions in MySQL are only capable of saying "yes that matches" or "no it doesn't".
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Creating and Managing Databases with phpMyAdmin in cPanel.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now