• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

MySQL STRING functions for BINARY formatting

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
willsherwood
Asked:
willsherwood
  • 2
  • 2
1 Solution
 
Terry WoodsIT GuruCommented:
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
 
willsherwoodAuthor Commented:
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
 
willsherwoodAuthor Commented:
... or maybe it's just a series of  
CONCAT(
   LEFT(BIN(FieldName),4),' ',
   MID(BIN(FieldName),5,4),' ',
     ...
   )
0
 
Terry WoodsIT GuruCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now