Post: [Java] BitConverter from C#
05-15-2011, 01:31 PM #1
kiwimoosical
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); So recently I noticed when I was writing my IO class in java, that there was no BitConverter, or any equivalent to it in java. Sooooo! I decided to make my own! Since java doesn't support unsigned anything (other than right shift), there are no conversions to and from UInt-s. Other than that it functions exactly like the BitConverter class in C# :]
All the methods are static so no instantiation is necessary. Just the below code in another class file named BitConverter.java and compile your code :]

Code:
    
import java.lang.Exception;

public class BitConverter
{
public static byte[] getBytes(boolean x)
{
return new byte[]{
(byte) (x ? 1:0)
};
}

public static byte[] getBytes(char c)
{
return new byte[] {
(byte)(c & 0xff),
(byte)(c >> 8 & 0xff) };
}

public static byte[] getBytes(double x)
{
return getBytes(
Double.doubleToRawLongBits(x));
}

public static byte[] getBytes(short x)
{
return new byte[] {
(byte)(x >>> Cool Man (aka Tustin),
(byte)x
};
}

public static byte[] getBytes(int x)
{
return new byte[] {
(byte)(x >>> 24),
(byte)(x >>> 16),
(byte)(x >>> Cool Man (aka Tustin),
(byte)x
};
}

public static byte[] getBytes(long x)
{
return new byte[] {
(byte)(x >>> 56),
(byte)(x >>> 4Cool Man (aka Tustin),
(byte)(x >>> 40),
(byte)(x >>> 32),
(byte)(x >>> 24),
(byte)(x >>> 16),
(byte)(x >>> Cool Man (aka Tustin),
(byte)x
};
}

public static byte[] getBytes(float x)
{
return getBytes(
Float.floatToRawIntBits(x));
}

public static byte[] getBytes(String x)
{
return x.getBytes();
}

public static long doubleToInt64Bits(double x)
{
return Double.doubleToRawLongBits(x);
}

public static double int64BitsToDouble(long x)
{
return (double)x;
}

public boolean toBoolean(byte[] bytes, int index) throws Exception
{
if(bytes.length != 1)
throw new Exception("The length of the byte array must be at least 1 byte long.");
return bytes[index] != 0;
}

public char toChar(byte[] bytes, int index) throws Exception
{
if(bytes.length != 2)
throw new Exception("The length of the byte array must be at least 2 bytes long.");
return (char)(
(0xff & bytes[index]) << 8 |
(0xff & bytes[index + 1]) << 0
);
}

public double toDouble(byte[] bytes, int index) throws Exception
{
if(bytes.length != Cool Man (aka Tustin)
throw new Exception("The length of the byte array must be at least 8 bytes long.");
return Double.longBitsToDouble(
toInt64(bytes, index));
}

public static short toInt16(byte[] bytes, int index) throws Exception
{
if(bytes.length != Cool Man (aka Tustin)
throw new Exception("The length of the byte array must be at least 8 bytes long.");
return (short)(
(0xff & bytes[index]) << 8 |
(0xff & bytes[index + 1]) << 0
);
}

public static int toInt32(byte[] bytes, int index) throws Exception
{
if(bytes.length != 4)
throw new Exception("The length of the byte array must be at least 4 bytes long.");
return (int)(
(int)(0xff & bytes[index]) << 56 |
(int)(0xff & bytes[index + 1]) << 48 |
(int)(0xff & bytes[index + 2]) << 40 |
(int)(0xff & bytes[index + 3]) << 32
);
}

public static long toInt64(byte[] bytes, int index) throws Exception
{
if(bytes.length != Cool Man (aka Tustin)
throw new Exception("The length of the byte array must be at least 8 bytes long.");
return (long)(
(long)(0xff & bytes[index]) << 56 |
(long)(0xff & bytes[index + 1]) << 48 |
(long)(0xff & bytes[index + 2]) << 40 |
(long)(0xff & bytes[index + 3]) << 32 |
(long)(0xff & bytes[index + 4]) << 24 |
(long)(0xff & bytes[index + 5]) << 16 |
(long)(0xff & bytes[index + 6]) << 8 |
(long)(0xff & bytes[index + 7]) << 0
);
}

public static float toSingle(byte[] bytes, int index) throws Exception
{
if(bytes.length != 4)
throw new Exception("The length of the byte array must be at least 4 bytes long.");
return Float.intBitsToFloat(
toInt32(bytes, index));
}

public static String toString(byte[] bytes) throws Exception
{
if(bytes == null)
throw new Exception("The byte array must have at least 1 byte.");
return new String(bytes);
}
}


Also, for those of you who are experienced Java programmers, I am starting a huge project and need good programmers, if you think you qualify for re-writing a lot of C#'s libraries msg me.
(adsbygoogle = window.adsbygoogle || []).push({});
05-16-2011, 02:32 PM #2
vSaBoTeuR x
< ^ > < ^ >
Looks nice!

I would've chipped in if this was for C++ or Python, haven't started with Java yet. Is it a good language?

The following user groaned vSaBoTeuR x for this awful post:

Josh_ox3
05-17-2011, 08:44 PM #3
kiwimoosical
Bounty hunter
Originally posted by xNCK View Post
Looks nice!

I would've chipped in if this was for C++ or Python, haven't started with Java yet. Is it a good language?


Of course it's a good language xD
It's going to be even better once my team finishes our C# to Java package Winky Winky

Also you can still help, if you understand the use of bitwise operators for conversions and what not.. like say you want to turn a byte[] into a short:
    short newthing = ((bytes[0] && 0xFF) << 8 | (bytes[1] & 0xFF));


If you know how to do that kind of stuff, then you can help in the long run probably
05-19-2011, 08:02 PM #4
vSaBoTeuR x
< ^ > < ^ >
Originally posted by kiwimoosical View Post
Of course it's a good language xD
It's going to be even better once my team finishes our C# to Java package Winky Winky

Also you can still help, if you understand the use of bitwise operators for conversions and what not.. like say you want to turn a byte[] into a short:
    short newthing = ((bytes[0] && 0xFF) << 8 | (bytes[1] & 0xFF));


If you know how to do that kind of stuff, then you can help in the long run probably


Well, I'll make some research on it and try to help you. Winky Winky

The following user groaned vSaBoTeuR x for this awful post:

Josh_ox3

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo