Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 08-20-2011, 03:38 AM
Junior Member
 
Join Date: 08-16-11
Posts: 19
iTrader: 0 / 0%
String or Char -> Binary Representation

Hi guys,

Is there a function to convert a String to its binary representation. Also maybe convert a char to binary.

Thank you.
 
Reply With Quote
  #2 (permalink)  
Old 08-20-2011, 06:22 AM
VonBestLeich's Avatar
Member
Latest Blog:
None

 
Join Date: 08-13-11
Posts: 49
iTrader: 0 / 0%
You didn't mention what programming language you're using, so I will assume it's C++. You can find a good way to do that in the following link:-
http://www.cplusplus.com/forum/general/1764/

Let me know if that's not what you are looking for.
__________________
Sweet Tech For You.
 
Reply With Quote
  #3 (permalink)  
Old 08-20-2011, 12:30 PM
Junior Member
 
Join Date: 08-16-11
Posts: 19
iTrader: 0 / 0%
Sorry

I am so sorry...
I totally forgot that
I am interested in PHP and JAVA

thanks
 
Reply With Quote
  #4 (permalink)  
Old 08-21-2011, 01:16 AM
VonBestLeich's Avatar
Member
Latest Blog:
None

 
Join Date: 08-13-11
Posts: 49
iTrader: 0 / 0%
Ah okay.

I don't know much about PHP, But in java you can start by converting the character to an Integer then use the Integer.toBinaryString(i); function to get the binary of the value of the integer like that:-

char ch='m';
int chValue=(int)ch;
String Binary=Integer.toBinaryString(chValue);

If the pure binary value is what you're looking for, you can use the getBytes function in the string you wish to convert to binary.

I hope that's useful.
__________________
Sweet Tech For You.
 
Reply With Quote
  #5 (permalink)  
Old 08-22-2011, 07:19 AM
Rukbat's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
iTrader: 0 / 0%
Are you going to do this server-side(PHP) or client-side (Javascript)? (BTW, Java and Javascript are totally different languages.) You can't do Javascript server-side and you can't do PHP client-side, so you first have to determine where the code is going to be running. "PHP or Javascript" just doesn't work - where the code runs determines which language it has to be.
 
Reply With Quote
  #6 (permalink)  
Old 08-23-2011, 12:59 AM
Junior Member
 
Join Date: 08-16-11
Posts: 19
iTrader: 0 / 0%
Thanks guys,

Yes I know that Java and JavaScript are different and JS is client-side.
I need the JAVA for a mobile application and the PHP for my website...
 
Reply With Quote
  #7 (permalink)  
Old 08-23-2011, 05:55 AM
The Controversial Coder
Latest Blog:
None

 
Join Date: 05-01-06
Location: Manchester; UK
Posts: 2,372
iTrader: 0 / 0%
Regular expressions and base convert can do this. A quick Google came up with various examples,

PHP Code:
$string "string1string2string3";
$string preg_replace ('/(\d+)/e''base_convert (\\1, 10, 2)'$string);

echo 
$string
Source - Hotscripts Forums.
 
Reply With Quote
  #8 (permalink)  
Old 08-25-2011, 02:48 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
there's a great custom function in the php notes, it might be useful to you...
http://www.php.net/manual/en/ref.strings.php#59622

PHP Code:
<?php
/**
 *    Returns an ASCII string containing 
 *    the binary representation of the input data .
**/
function str2bin($str$mode=0) {
    
$out false;
    for(
$a=0$a strlen($str); $a++) {
        
$dec ord(substr($str,$a,1));
        
$bin '';
        for(
$i=7$i>=0$i--) {
            if ( 
$dec >= pow(2$i) ) {
                
$bin .= "1";
                
$dec -= pow(2$i);
            } else {
                
$bin .= "0";
            }
        }
        
/* Default-mode */
        
if ( $mode == $out .= $bin;
        
/* Human-mode (easy to read) */
        
if ( $mode == $out .= $bin " ";
        
/* Array-mode (easy to use) */
        
if ( $mode == $out[$a] = $bin;
    }
    return 
$out;
}
?>
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to restore db,upload the .sql file to new host using ftp, using ASCII or binary? basketmen Web Hosting Forum 3 09-23-2010 05:12 PM
hosting Int-char.com Int-char.com Web Hosting 0 12-18-2008 04:06 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 08:49 PM.
Powered by vBulletin
Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.