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


Groups > comp.lang.c > #77136 > unrolled thread

convert unsigned long long into char[4]

Started bybilsch01 <king621@comcast.net>
First post2015-11-24 15:40 -0800
Last post2015-11-28 22:06 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  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

#77136 — convert unsigned long long into char[4]

Frombilsch01 <king621@comcast.net>
Date2015-11-24 15:40 -0800
Subjectconvert unsigned long long into char[4]
Message-ID<n32sd3$qjh$1@dont-email.me>
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;
}

[toc] | [next] | [standalone]


#77140

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-11-24 17:21 -0800
Message-ID<8h2a5bhisidqqqoejb1cte915mleco6v55@4ax.com>
In reply to#77136
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.

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

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

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#77141

Frombilsch01 <king621@comcast.net>
Date2015-11-24 18:10 -0800
Message-ID<n3356o$kio$1@dont-email.me>
In reply to#77140
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.
>

[toc] | [prev] | [next] | [standalone]


#77142

FromKeith Thompson <kst-u@mib.org>
Date2015-11-24 18:18 -0800
Message-ID<ln610q2559.fsf@kst-u.example.com>
In reply to#77141
bilsch01 <king621@comcast.net> writes:
> On 11/24/2015 05:21 PM, Barry Schwarz wrote:
[...]
>> 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 int is (at least) 32 bits, then 0x46464645 is of type int.

If not, then it's of type long.

(It's barely possible that it's of type unsigned int, but that would
require an implementation where int has only 30 value bits.  As far as I
know, no such ipmlementations exist.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#77150

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2015-11-25 00:23 -0800
Message-ID<83f3a05d-3535-4791-9c8a-aa92b57178cd@googlegroups.com>
In reply to#77136
On Tuesday, November 24, 2015 at 11:40:48 PM UTC, bilsch01 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;
> }


unsigned long long d2hx(int dc)
{
  char hex[5];
  int i;
  unsigned long long answer = 0;
  assert(dc >= 0 && dc <= 0xFFFF);
  
  for(i=0;i<4;i++)
  {
    hex[i] = "0123456789abcdef"[(dc >> ((3-i) * 4)) & 0xF];
  }
  hex[i] = 0;
  memcpy(&answer, hex, 5);

  return answer;
}

 int main(){
  int dc = 0xfffe;
  unsigned long long answer;

 answer = d2hx(dc);
 // bug here, add the &
 printf("answer = %s\n", &answer);
 return 0;
}

[toc] | [prev] | [next] | [standalone]


#77156

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-11-25 11:54 +0000
Message-ID<8737vui9a1.fsf@bsb.me.uk>
In reply to#77150
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On Tuesday, November 24, 2015 at 11:40:48 PM UTC, bilsch01 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'
<snip>
> unsigned long long d2hx(int dc)
> {
>   char hex[5];
>   int i;
>   unsigned long long answer = 0;
>   assert(dc >= 0 && dc <= 0xFFFF);
>   
>   for(i=0;i<4;i++)
>   {
>     hex[i] = "0123456789abcdef"[(dc >> ((3-i) * 4)) & 0xF];

(The OP wants "ABCDEF".)

>   }
>   hex[i] = 0;
>   memcpy(&answer, hex, 5);

That might not produce the value the OP wants.  By writing 0x46464645
there is an indication that the result should be a specific integer
value.

If that's the case, I'd do it by arithmetic rather than memcpy:

  answer = (answer << 8) + "0123456789ABCDEF"[(dc >> (3-i) * 4) & 0xF];

>   return answer;
> }
>
>  int main(){
>   int dc = 0xfffe;
>   unsigned long long answer;
>
>  answer = d2hx(dc);
>  // bug here, add the &
>  printf("answer = %s\n", &answer);

It's not clear what the bug is.  It might be that the op intended to
print a hex value rather than a string.  Also, though it's rare to care
about portability in code like this, you should a cast to that argument
or all bets are, technically, off.  It's undefined behaviour as it is.

>  return 0;
> }

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#77182

Frombilsch01 <king621@comcast.net>
Date2015-11-25 11:35 -0800
Message-ID<n352ea$vhg$1@dont-email.me>
In reply to#77150
On 11/25/2015 12:23 AM, Malcolm McLean wrote:
> On Tuesday, November 24, 2015 at 11:40:48 PM UTC, bilsch01 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;
>> }
>
>
> unsigned long long d2hx(int dc)
> {
>    char hex[5];
>    int i;
>    unsigned long long answer = 0;
>    assert(dc >= 0 && dc <= 0xFFFF);
>
>    for(i=0;i<4;i++)
>    {
>      hex[i] = "0123456789abcdef"[(dc >> ((3-i) * 4)) & 0xF];

I've never seen:  "character list"[bitwise and]
I realize what is intended but I've never seen the two things interact 
like that.
>    }
>    hex[i] = 0;
>    memcpy(&answer, hex, 5);
>
>    return answer;
> }
>
>   int main(){
>    int dc = 0xfffe;
>    unsigned long long answer;
>
>   answer = d2hx(dc);
>   // bug here, add the &
>   printf("answer = %s\n", &answer);
>   return 0;
> }
>

Thanks.

[toc] | [prev] | [next] | [standalone]


#77349

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2015-11-28 22:06 +0000
Message-ID<slrnn5k9av.5q5.grahn+nntp@frailea.sa.invalid>
In reply to#77136
On Tue, 2015-11-24, bilsch01 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'.

> extern unsigned long long d2hx(int dec);

Don't do that -- don't pretend an unsigned long long can be seen as
four (or eight or whatever) ASCII bytes.  Don't fight the type system,
unless you have very special needs.  I would have made d2hx() fill in
a buffer:

  char* d2hx(char* buf, size_t len, int buf);

(Returning the buffer as a bonus -- often useful in practice.)

But as others wrote, snprintf() already does this better.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web