Saturday 5 October 2019

Put leading zeros for long type number in java




My client sends me some data via web service, which he created, and expects 4 digit long value as return code. For example return code 9461 is for failure of operation A, or return code 0000 is for success. There is no problem of returning error codes, however when return code is 0000, that is success I am facing the following problem.



I can't create 4 digit long which starts with 0. I tried to set webservice response as follows




response.setResponseCode(0000L);


However it sends 0 instead of 0000. I know I can use hardcoded "0000" or String.format or DecimalFormat, but setResponseCode takes long as input, therefore I can't use string.



Is there any way to set zeros before number so that 0 can be set like 0000 or 45 can be set like 0045?


Answer



Simply no. 0000 and 0 have the same byte representation if you treat it as long.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...