Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #77141

Re: convert unsigned long long into char[4]

From bilsch01 <king621@comcast.net>
Newsgroups comp.lang.c
Subject Re: convert unsigned long long into char[4]
Date 2015-11-24 18:10 -0800
Organization A noiseless patient Spider
Message-ID <n3356o$kio$1@dont-email.me> (permalink)
References <n32sd3$qjh$1@dont-email.me> <8h2a5bhisidqqqoejb1cte915mleco6v55@4ax.com>

Show all headers | View raw


On 11/24/2015 05:21 PM, Barry Schwarz wrote:
> On Tue, 24 Nov 2015 15:40:31 -0800, bilsch01 <king621@comcast.net>
> wrote:
>
>> See the code below.  The function d2hx takes a 4 digit hex number 0xfffe
>> and converts it into ascii bytes returned in unsigned long long
>> 'answer'.  The contents of answer is 0x46464645
>> where 0x46 = 'F' and 0x45 = 'E'
>> In the C code below I need to convert 'answer' into type char[4] (plus
>> null termination will be char[5]) and print it with printf.  The result
>> will be FFFE  I don't know how to convert unsigned long long into char[4].
>>
>> TIA.    Bill S.
>>
>>
>> #include  <stdio.h>
>>
>> extern unsigned long long d2hx(int dec);
>>
>> int main(){
>> int dc = 0xfffe;
>> unsigned long long answer;
>>
>> answer = d2hx(dc);
>> printf("answer = %s\n", answer);
>> return 0;
>> }
>
> Why is dc an unsigned long long?  It should fit in an unsigned int on
> most current systems.

dc is int
0x46464645 is not int

>
> If all you want to do is print the value, then
>       printf("... = %.4X\n", (unsigned int)dc);
> should work.

I didn't know that format specifier.  Thanks, that saves a lot of work.

>
> If you really need the result in a string, the same approach using
> sprintf should suffice.
>

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

convert unsigned long long into char[4] bilsch01 <king621@comcast.net> - 2015-11-24 15:40 -0800
  Re: convert unsigned long long into char[4] Barry Schwarz <schwarzb@dqel.com> - 2015-11-24 17:21 -0800
    Re: convert unsigned long long into char[4] bilsch01 <king621@comcast.net> - 2015-11-24 18:10 -0800
      Re: convert unsigned long long into char[4] Keith Thompson <kst-u@mib.org> - 2015-11-24 18:18 -0800
  Re: convert unsigned long long into char[4] Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-25 00:23 -0800
    Re: convert unsigned long long into char[4] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-25 11:54 +0000
    Re: convert unsigned long long into char[4] bilsch01 <king621@comcast.net> - 2015-11-25 11:35 -0800
  Re: convert unsigned long long into char[4] Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-28 22:06 +0000

csiph-web