Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

how to use hex escape char with string in C?

Programming


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-27-2010
vistastar's Unix or Linux Image
vistastar vistastar is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 August 2016, 2:09 AM EDT
Location: Galaxy
Posts: 180
Thanks: 7
Thanked 10 Times in 10 Posts
how to use hex escape char with string in C?

I want it to ouput "abcd", but it dosen't.


Code:
  1 #include<stdio.h>
  2 int main()
  3 {
  4         printf("a\x62cd");
  5 }
  6

gcc alarm.c -o alarm
alarm.c: In function 'main':
alarm.c:4:9: warning: hex escape sequence out of range

It seems that the complier joint "cd" as part of \x62.
Maybe I can write it like this:

Code:
printf("a\x62""cd");

but it is a bit urgly...

Last edited by vistastar; 11-27-2010 at 12:28 AM..
Sponsored Links
    #2  
Old Unix and Linux 11-27-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 October 2017, 3:09 PM EDT
Location: Saskatchewan
Posts: 22,469
Thanks: 1,143
Thanked 4,262 Times in 3,936 Posts
That's what I ended up having to do before. That, or seperating it out of the string entirely as in printf("a%ccd", 0x62);
Sponsored Links
    #3  
Old Unix and Linux 11-27-2010
vistastar's Unix or Linux Image
vistastar vistastar is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 29 August 2016, 2:09 AM EDT
Location: Galaxy
Posts: 180
Thanks: 7
Thanked 10 Times in 10 Posts
Why don't the complier just translates the fixed few of characters(eg: two char) after \x to an escape char?
    #4  
Old Unix and Linux 11-29-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 October 2017, 3:09 PM EDT
Location: Saskatchewan
Posts: 22,469
Thanks: 1,143
Thanked 4,262 Times in 3,936 Posts
Why should it? There are systems where "char" isn't 8 bits... Or were, decades ago, and the C standard people latch onto this technicality like a bear trap and refuse to let go.

It plainly doesn't assume it stops at 8 bits, anyway, so the question's kind of pointless.
Sponsored Links
    #5  
Old Unix and Linux 11-29-2010
achenle achenle is offline Forum Advisor  
Registered User
 
Join Date: Jun 2009
Last Activity: 3 October 2017, 8:53 AM EDT
Posts: 1,008
Thanks: 3
Thanked 154 Times in 146 Posts
Unicode characters aren't 8 bits, and they've been spotted in use somewhat more recently than decades ago.

The only way to produce a deterministic result when the number of output bits can not be constrained is with a maximal-munch parser.
Sponsored Links
    #6  
Old Unix and Linux 11-30-2010
niterobin's Unix or Linux Image
niterobin niterobin is offline
Registered User
 
Join Date: Oct 2006
Last Activity: 18 March 2015, 9:55 AM EDT
Location: Stevenage, UK.
Posts: 15
Thanks: 16
Thanked 0 Times in 0 Posts
I tried your code with the same result as you. Then I just tried the following code:


Code:
  1 #include<stdio.h>
  2 int main()
  3 {
  4         printf("a\x62mn");
  5 }
  6

with the output:

Code:
#rob@fred:~/programs$./unixcom.exee
abmn
rob@fred:~/programs$

I think what is happening with your code is that the compiler is reading the \x62cd as one hex number - \xbcd. I would guess that Corona688's suggestion - printf("a%ccd", 0x62) - is your best bet.

HTH,

Rob.
Sponsored Links
    #7  
Old Unix and Linux 11-30-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 October 2017, 7:54 PM EDT
Location: NM
Posts: 11,215
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
achenle - unicode or utf-16 or whatever else - have been around for a long time. They are not considered 'char' in C, they are wchar, wide characters, a different datatype. This datatype affects file orientation, which is the primary way reads & writes occur on tyy/file/device(s). wchar are NOT char, by definition and practice

try man wchar

And there still are embedded systems with 32 bit char.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting URL request string including escape characters john2022 Shell Programming and Scripting 4 09-04-2009 10:19 AM
Remove escape characters from string gio001 UNIX for Advanced & Expert Users 2 07-11-2009 08:10 AM
sed escape char fed.linuxgossip Shell Programming and Scripting 2 10-04-2008 06:41 AM
Escape Char for double quote navik_pathak Shell Programming and Scripting 1 01-05-2007 01:34 PM



All times are GMT -4. The time now is 12:30 AM.