1

I am trying to scan a short int (16 bits/2 bytes) from a memory pointer using sscanf as shown below. But it is showing some weird behaviour.

#include <stdio.h>
#include <errno.h>

int main()
{
    unsigned char p[] = {0x00, 0x1A};
    short int s = 0;
    int ret = sscanf(p,"%hu",&s);
    printf("Actual data %02x %02x\n",p[0],p[1]);
    printf("s = %hu ret = %d errno = %d\n",s,ret,errno);
    return 0;
}

Output:

Actual data 00 1a
s = 0 ret = -1 errno = 0

Please help me to understand what I am doing wrong !

Paul R
  • 208,748
  • 37
  • 389
  • 560
Sunny Shukla
  • 537
  • 1
  • 6
  • 17
  • 1
    Note that it is [redundant and potentially dangerous to cast the result of malloc and friends in C](http://stackoverflow.com/q/605845/253056). – Paul R Aug 12 '16 at 13:54
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Aug 12 '16 at 13:55
  • 2
    `p_extradata` is pointing at a `0x00` byte, so it looks like an empty string, hence there is nothing for `sscanf` to parse. Maybe you just want to *copy* those two bytes verbatim to your `short`, not convert a C string to a `short` ? – Paul R Aug 12 '16 at 13:56
  • `sscanf()` with `%hu` and corresponding `(signed) short*` doesn't look good. – EOF Aug 12 '16 at 13:58
  • @EOF: true, but I don't think that's the main problem here. – Paul R Aug 12 '16 at 13:59
  • @PaulR - why not post it as an answer? – Support Ukraine Aug 12 '16 at 13:59
  • @4386427: I would, but I am waiting for clarification from the OP as to what they actually want to achieve. They seem to have disappeared now though. – Paul R Aug 12 '16 at 14:00
  • Edited the question so that results can be reproduced. – Sunny Shukla Aug 12 '16 at 14:03

1 Answers1

2

p is pointing at a 0x00 byte, so it looks like an empty string, hence there is nothing for sscanf to parse.

It looks like just want to copy those two bytes verbatim to your short, not convert a C string to a short ? If so then instead of the sscanf call you could just use memcpy:

memcpy(&s, p, sizeof(s));

Note that this assumes that your buffer and the destination short have the same endianness. If not then you will also need to take care of the byte order.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • As I have edited the question the correct answer will be memcpy(&s, p, sizeof(s)); – Sunny Shukla Aug 12 '16 at 14:10
  • Updated - in future please don't radically alter the question once there are valid answers to the original question. – Paul R Aug 12 '16 at 14:11
  • I was editing and the answers were already on their way, so once I saved I saw your answer. But then to revert will be not ok. So I kept it changed. BTW Thanks. – Sunny Shukla Aug 12 '16 at 14:13
  • No problem - it was only a minor update anyway - just something to watch out for in future. – Paul R Aug 12 '16 at 14:14
  • One more thing when I used "unsigned char p[] = {0x1B, 0x1A};" still the sscanf doesn't work. So that means sscanf does need a proper string terminated with \0 (NULL) to work with. – Sunny Shukla Aug 12 '16 at 14:29
  • 1
    @SunnyShukla: `sscanf` is for parsing/converting *strings* into data - it looks like you just have *raw bytes*, so you just need to *copy* the data, not parse/convert it. – Paul R Aug 12 '16 at 14:30