Thread: converting string to byte in c++

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    converting string to byte in c++

    is there any way to convert string to byte in c++ just like the getbytes() in java??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like using the [ ] operator perhaps?
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    int main ( ) {
        std::string foo = "hello";
        std::cout << "Letter=" << foo[0] << std::endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Well, getBytes() returns an array of bytes, so I think the closest to that would be using string::c_str() to get a pointer and copy the characters to a new char array (or copy them using operator[]).

    Code:
    std::string str = "hello world";
    char* cstr = new char [str.size() + 1];
    std::strcpy(cstr, str.c_str());
    //...
    delete [] cstr;
    Or even better, use a vector like Elysia suggested below.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget to delete that!
    A better approach:
    Code:
    std::string str = "Hello World";
    std::vector<char> bytes(str.begin(), str.end());
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    thanks for the help and sorry for the late reply but i was thinking of a little something like this one

    Code:
    public class TestByte
    {    
    	public static void main(String[] argv) {
     
    		    String example = "This is an example";
    		    byte[] bytes = example.getBytes();
     
    		    System.out.println("Text : " + example);
    		    System.out.println("Text [Byte Format] : " + bytes);
     
     
    	}
    }
    The output is

    Text : This is an example
    Text [Byte Format] : [B@187aeca

    is this possible to do in c++?

  6. #6
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    A "byte format" baffles me. You do realize that "[B@187aeca" is just a textual representation of the variable. "[B" means its an array of bytes, and the rest of it is probably an address. It's not what you'd get if you'd loop over the array and print it byte by byte.

    So, you already have "bytes" available in a string trough operator[], and if you want to have an array or a vector of bytes/chars, then just use the code above. I fear, though, that you don't quite get the whole shebang with c-strings/char arrays and std::string in the first place. In this case I think you should read trough these:
    Character Sequences - C++ Documentation
    C Strings - C Tutorial - Cprogramming.com
    Cprogramming.com - C++ Standard Library - String Class

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    thanks..not the answer i am looking for but it helps in another way..

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then what do you want? You have your array of bytes (examples above).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A Java String is a sequence of Java chars, which are UTF-16 codepoints. getBytes() without arguments takes the platform default encoding (some Windows-xxxx codepage under Windows, UTF-8 on most Unices) and converts the Unicode sequence to that encoding, returning the resulting data as a byte array.

    A C++ std::string already is a sequence of bytes in the platform default encoding (strictly speaking, it's a sequence of bytes whose interpretation is up to you, but this is the way most library functions treat it), so your question makes no sense. There's nothing to convert.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Perhaps the best question is, what are you trying to do?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Convert a byte datatype to char or string
    By kvp_vivek in forum C Programming
    Replies: 3
    Last Post: 06-17-2006, 02:20 PM
  2. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 04:59 PM
  3. Converting an 8-Byte Array into a Long Representation
    By maththeorylvr in forum C Programming
    Replies: 11
    Last Post: 06-17-2005, 03:21 PM
  4. Adding a byte to a string
    By psul in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2004, 02:11 PM
  5. Converting byte arrays to vectors
    By kasun in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2004, 10:31 AM

Tags for this Thread