Image hash's output String always has "null" at the beginning

I'm trying to hash an image and show the hash result in string in text field, the code is working but the result always has "null" at the beginning of the hash code.

bitmap1 = BitmapFactory.decodeFile(picturePath1,opt); ImageView view1 = (ImageView) findViewById(R.id.gambar5); view1.setImageBitmap(bitmap1); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap1.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byte[] data1 = outputStream.toByteArray(); MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(data1); byte[] hash = md.digest(); String result = returnHex(hash); EditText et1 = (EditText) findViewById(R.id.editText1); et1.setText(result);

I'm using returnHex function from this site returnHex

this is the function code

static String returnHex(byte[] inBytes) throws Exception { String hexString = null; for (int i=0; i < inBytes.length; i++) { //for loop ID:1 hexString += Integer.toString( ( inBytes[i] & 0xff ) + 0x100, 16).substring( 1 ); }// Belongs to for loop ID:1 return hexString; }

the result is always return with "null" like an ex: null8984fb2c1ba6ec6571d365553e466c95c94eba9d6fb2079af03

My question is: 1. What is the "null" that always show up? Is it a part of the hash code or a bug? 2. If "null" is a bug, how I can make it not included in the result?

-------------Problems Reply------------

You set the string to null then append to it; what else could happen?

Set it to an empty string to initialize it, e.g., String hexString = "";

Category:java Views:3 Time:2017-09-07
Tags: java hash android

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.121 (s). 10 q(s)