1

I have a csv string to bytes and i want to convert it in java byte array. Can anyone help me.

csv string

167, 27, 32, 195

The byte array should be like this

byte[0] should give me 167
byte[1] should give me 27
byte[2] should give me 32
byte[3] should give me 195
Salman
  • 1,380
  • 7
  • 25
  • 41

1 Answers1

1

You can use an array of characters. The char type is intended to represent characters in Java.

String csv = "167, 27, 32, 195";
String[] numbers = csv.split(", ");
char[] chars = new char[numbers.length];
for (int i = 0; i < numbers.length; i++)
    chars[i] = (char)Integer.parseInt(numbers[i]);

This method assumes that the numbers in the CSV file are Unicode code points of the characters.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • Its giving an error on `(char)numbers[i];` **cannot cast String to char**, well i have already fixed the problem you can see in comments of the question – Salman Jul 19 '12 at 07:22