Java Convert via ByteBuffer stringToBytes(String str)

Here you can find the source of stringToBytes(String str)

Description

string To Bytes

License

Open Source License

Declaration

public static byte[] stringToBytes(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static byte[] stringToBytes(String str) {
        StringBuffer sb = new StringBuffer(str);
        char c = sb.charAt(0);
        ByteBuffer buffer = ByteBuffer.allocate(sb.length() * 2);
        int index = 0;

        while (index < sb.length()) {
            buffer.putChar(sb.charAt(index++));
        }/*  w  w  w  .  j  av a 2 s  . c o m*/

        return buffer.array();
    }
}

Related

  1. shortToByteArray(short inShort)
  2. shortToBytes(short s)
  3. stringsFromBinary(final byte[] binary)
  4. stringsToBinary(final String strings[])
  5. stringToBytes(final String inString)
  6. StringToData(String conv, boolean isRe, int number)
  7. stringToIpAddress(String s)
  8. stringToLong(String offsetString)
  9. stringToLongUnknownLength(String str, int startIndex)