473,513 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sprintf() in java

2 New Member
Hi,
I want to get the sprintf() function in java.
Please write the code of the following sprintf() function in java.

sprintf(buff,"%2.2X ",RValue);

Here buff is a string variable and RValue is also a string variable. The middle parameter is the format. My question is how to store the value of RValue in buff using the format specified("%2.2X ").
Nov 19 '08 #1
1 12153
chaarmann
785 Recognized Expert Contributor
Strings are immutable in Java. So you have to use StringBuffer or StringBuilder for the "buff" result. (Another way would be to use it with different syntax:
String buff=sprintf(format, RValue);)
Java has PrintStream.printf() method that works similar. Now you have to redirect the output of the PrintStream into a StringBuilder.

Another alternative is the MessageFormat.format() method.

If you google then you can see that there are also a lot free libraries that contain a sprintf-like function for java. You can download the source code for them (or decompile) and see how they did it.

Oh, i forgot: if you use the new Java 5 then you have a new method inside the String class:
format(String format, Object ... args). You can use this as an sprintf equivalent.
Also have a look at Formatter class.
But if you must use an older version of Java, then only the MessageFormat.format() and its derived classes can help you (or the direct work with StringBuffer).
Nov 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
4989
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution string. I all works fine, even the sprintf, when I write numbers. But if I want to write a character string, it all goes wrong. I am trying to print...
32
440
by: Michele | last post by:
I have a code line like this: sprintf(buf, "%20g",*(liftPtr++)) what does it mean? And what does the function sprintf do? Thanks michele
3
42108
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf, "0x%02x", n); The above code worked. Howeve, what I am trying to do is to convert an
6
2481
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make the character array(alerts.msg) with sprintf doesn't work for the obvious reasons in my first sentence with tmp having those control characters. Is...
1
3509
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some others are asked below: 1. #include <stdio.h> int main(){ char buffer;
2
4707
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
12
4586
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ??? sprintf(&chData, "%+05.0f", -0.380f); --I get "-000" ??? sprintf(&chData, "%+05.0f", -3.800f); --I get "-0004" ok
15
3503
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it PrintOnConsole(), to get the output on a console. The problem with PrintOnConsole() is that it only takes strings as input arguments. On the other...
3
2601
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2", str); //Interesting stuff happens here printf(str2);
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.