473,972 Members | 34,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sprintf: the format problem

Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);

No error! Just no working. Well, I used sprintf() a lot in format "%s"
"%d", and liked sprintf. It is my first time of using it in format
"%x". I wonder does format affect sprintf? Anybody can help me to get
this trick? Thanks!

Huey
Nov 14 '05 #1
2 2540
Huey wrote:
Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);


I understand very little of what you've said, but if buf is an array
then you certainly cannot use it this way in a sprintf call. The %x
format specifier applies *ONLY* to unsigned int. If you pass anything
else, the behavior is undefined.

I would try to suggest an alternative, but I have no idea what you are
trying to do.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

"Huey" <hu********@yah oo.com> schrieb im Newsbeitrag
news:ae******** *************** ***@posting.goo gle.com...
Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);
Try
(CHAR_BIT == 8 assumed)

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
int i;
unsigned char buf[32] =
{0, 1, 2, 3, 4, 5, 55, 66, 77};
char cbuf[97];

for(i = 0; i < 32; i++)
{
sprintf(cbuf + 3 * i, "%02x ", buf[i]);
}
puts(cbuf);
return EXIT_SUCCESS;
}


No error! Just no working. Well, I used sprintf() a lot in format "%s"
"%d", and liked sprintf. It is my first time of using it in format
"%x". I wonder does format affect sprintf? Anybody can help me to get
this trick? Thanks!


from N869:
o,u,x,X The unsigned int argument is converted to unsigned octal (o),
unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd;
the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is
expanded
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1914
by: Pilatus | last post by:
Hi everybody, I am beginner and I have a problem in this simple code : I want that the char contents 01234567, but it don't work Could you help me ? int bin ; char *buf = new char ; for (int i = 0; i <= 7; i++) { tab = i ;
1
3536
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
4735
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
4
2384
by: ypjofficial | last post by:
Hello All, To use sprintf function we have to first create a char * and assign some memory to it or we have to fixed memory sized array. eg. char str;//or it can be //char * str =(char *)malloc(512); int d=100; float f=2.22;
10
4211
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece of code.. ---------------------------- long myLong=200000; char myStr;
12
4625
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
6
11649
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle this exception as they like and finally show a message to the user or not. Only in a few occasions I need some special exception types to behave differently depending on the error. So what you basically do is
15
3549
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 hand, I'm free to use sprintf(), so I can convert everything I want to print into a string and then...
3
2631
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
11805
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11399
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11555
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10067
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8451
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7599
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6402
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5143
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
3
3749
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.